自动将ASP.NET FormView绑定到新插入的记录
发布时间:2020-12-16 03:46:45 所属栏目:asp.Net 来源:网络整理
导读:是否有可能让FormView在其刚刚通过其InsertItemTemplate插入的记录上自动默认为ReadOnly模式?看起来这应该是一些应该自然而然地出现在FormView上的东西. 解决方法 对的,这是可能的. 我用Employee表演示它 使用以下Sql Server脚本创建名为tbemployee的employ
是否有可能让FormView在其刚刚通过其InsertItemTemplate插入的记录上自动默认为ReadOnly模式?看起来这应该是一些应该自然而然地出现在FormView上的东西.
解决方法
对的,这是可能的.
我用Employee表演示它 使用以下Sql Server脚本创建名为tbemployee的employee tabled CREATE TABLE [dbo].[tbemployee]( [empid] [int] IDENTITY(1,1) NOT NULL,[ename] [varchar](50) NULL,[eadd] [varchar](50) NULL,[esal] [int] NULL,[edno] [int] NULL,CONSTRAINT [PK_tbemployee] PRIMARY KEY CLUSTERED ( [empid] ASC ) ) ON [PRIMARY] GO 将记录插入FormView的源代码如下所示: FormView.aspx <asp:FormView ID="FormView1" runat="server" DataKeyNames="empid" oniteminserting="FormView1_ItemInserting" DefaultMode="Insert" onmodechanging="FormView1_ModeChanging"> <InsertItemTemplate> ename: <asp:TextBox ID="txtename" runat="server" Text='<%# Bind("ename") %>' /> <br /> eadd: <asp:TextBox ID="txteadd" runat="server" Text='<%# Bind("eadd") %>' /> <br /> esal: <asp:TextBox ID="txtesal" runat="server" Text='<%# Bind("esal") %>' /> <br /> edno: <asp:TextBox ID="txtedno" runat="server" Text='<%# Bind("edno") %>' /> <br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" /> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </InsertItemTemplate> <ItemTemplate> empid: <asp:Label ID="empidLabel" runat="server" Text='<%# Eval("empid") %>' /> <br /> ename: <asp:Label ID="enameLabel" runat="server" Text='<%# Bind("ename") %>' /> <br /> eadd: <asp:Label ID="eaddLabel" runat="server" Text='<%# Bind("eadd") %>' /> <br /> esal: <asp:Label ID="esalLabel" runat="server" Text='<%# Bind("esal") %>' /> <br /> edno: <asp:Label ID="ednoLabel" runat="server" Text='<%# Bind("edno") %>' /> <br /> <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" /> </ItemTemplate> </asp:FormView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=gts7;Initial Catalog=dbemp14;Integrated Security=True;Pooling=False" ProviderName="System.Data.SqlClient"> </asp:SqlDataSource> 并在FormView.aspx页面的代码后面粘贴以下代码: FormView.aspx.cs using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Configuration; public partial class _Default : System.Web.UI.Page { SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString); protected void Page_Load(object sender,EventArgs e) { } protected void FormView1_ItemInserting(object sender,FormViewInsertEventArgs e) { string ename,eadd,insertqry; Int32 esal,edno; ename = ((TextBox)(FormView1.FindControl("txtename"))).Text; eadd = ((TextBox)(FormView1.FindControl("txtename"))).Text; esal = Convert.ToInt32(((TextBox)(FormView1.FindControl("txtesal"))).Text); edno = Convert.ToInt32(((TextBox)(FormView1.FindControl("txtedno"))).Text); insertqry="insert tbemployee(ename,esal,edno) values(@ename,@eadd,@esal,@edno)"; if (con.State == ConnectionState.Closed) { con.Open(); } SqlCommand cmd = new SqlCommand(insertqry,con); cmd.Parameters.Add("@ename",SqlDbType.VarChar,50).Value = ename; cmd.Parameters.Add("@eadd",50).Value = eadd; cmd.Parameters.Add("@esal",SqlDbType.Int).Value = esal; cmd.Parameters.Add("@edno",SqlDbType.Int).Value = edno; cmd.ExecuteNonQuery(); cmd.Dispose(); con.Close(); FormView1.ChangeMode(FormViewMode.ReadOnly); formbind(); } public void formbind() { if (FormView1.AllowPaging == true) { SqlDataAdapter adp = new SqlDataAdapter("select * from tbemployee",con); DataSet ds = new DataSet(); adp.Fill(ds); Int32 totrecords = ds.Tables[0].Rows.Count; Int32 currentpageindex = totrecords - 1; FormView1.PageIndex = currentpageindex; FormView1.DataSource = ds; FormView1.DataBind(); } else { SqlDataAdapter adp = new SqlDataAdapter("select * from tbemployee where empid in (select isnull(max(empid),0) from tbemployee)",con); DataSet ds = new DataSet(); adp.Fill(ds); FormView1.DataSource = ds; FormView1.DataBind(); } } protected void FormView1_ModeChanging(object sender,FormViewModeEventArgs e) { FormView1.ChangeMode(e.NewMode); formbind(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在Visual Studio中使用TypeScript在lib.d.t
- asp.net-mvc – 如何在单击Html.ActionLink时加载局部视图?
- asp.net-web-api – 如何为代理控制器设置Web API路由?
- 61条面向对象设计的经验原则 转载
- asp.net-mvc – ASP.NET MVC 3 – ViewModel最佳实践
- 使用ASP.NET Core2.2创建WebApp
- asp.net-core – 哪些版本的Windows Server与.NET Core Run
- asp.net-mvc – 如何根据设备类型更改ASP.NET MVC视图?
- 文本框导致ASP.NET C#SQL出现问题
- asp.net-mvc – 捕获路由错误
推荐文章
站长推荐
热点阅读