获取GridView内TextBox控件的值

前台代码:

代码
   
     
1 < asp:GridView ID = " NianDaiGridView " runat = " server " AllowPaging = " True "
2 AutoGenerateColumns = " False " BorderWidth = " 1px " CellPadding = " 4 "
3 ForeColor = " #333333 " Width = " 100% " DataKeyNames = " niandaid " PageSize = " 8 "
4 EnableModelValidation = " True "
5 onpageindexchanging = " NianDaiGridView_PageIndexChanging "
6 onrowcreated = " NianDaiGridView_RowCreated "
7 onrowcancelingedit = " NianDaiGridView_RowCancelingEdit "
8 onrowediting = " NianDaiGridView_RowEditing "
9 onrowupdating = " NianDaiGridView_RowUpdating "
10 onrowdeleting = " NianDaiGridView_RowDeleting " >
11 < RowStyle BackColor = " #EFF3FB " HorizontalAlign = " Center " />
12 < Columns >
13 < asp:BoundField DataField = " niandaid " HeaderText = " 编号 " ReadOnly = " True " />
14 < asp:TemplateField HeaderText = " 朝代国家 " >
15 < ItemTemplate >
16 < asp:Label ID = " lblcaodaim " runat = " server " Text = ' <%#Eval("caodaim") %> ' ></ asp:Label >
17 < asp:Label ID = " lblguoming " runat = " server " Text = ' <%#Eval("guoming") %> ' ></ asp:Label >
18 </ ItemTemplate >
19 < EditItemTemplate >
20 < asp:TextBox ID = " txtcaodaim " runat = " server " Text = ' <%#Eval("caodaim") %> ' Width = " 50px " ></ asp:TextBox >
21 < asp:TextBox ID = " txtguoming " runat = " server " Text = ' <%#Eval("guoming") %> ' Width = " 80px " ></ asp:TextBox >
22 </ EditItemTemplate >
23 </ asp:TemplateField >
24 < asp:TemplateField HeaderText = " 起止年代(年) " >
25 < ItemTemplate >
26 < asp:Label ID = " lblqizhi " runat = " server " Text = ' <%#Eval("qizhi") %> ' ></ asp:Label >
27 </ ItemTemplate >
28 < EditItemTemplate >
29 < asp:TextBox ID = " txtqizhi " runat = " server " Text = ' <%#Eval("qizhi") %> ' ></ asp:TextBox >
30 </ EditItemTemplate >
31 </ asp:TemplateField >
32 < asp:CommandField HeaderText = " 编辑 " ShowEditButton = " True " />
33 < asp:TemplateField HeaderText = " 删除 " >
34 < ItemTemplate >
35 < asp:LinkButton ID = " LinkButton1 " runat = " server "
36 CommandArgument = ' <%#Eval("niandaid") %> ' CommandName = " GetDeleteByID "
37 OnClientClick = " return confirm(&quot;您真的要删除这条记录吗?&quot;) " Text = " 删除 " CausesValidation = " False " > 删除 </ asp:LinkButton >
38 </ ItemTemplate >
39 </ asp:TemplateField >
40 </ Columns >
41 < FooterStyle BackColor = " #507CD1 " Font - Bold = " True " ForeColor = " White " />
42 < PagerStyle BackColor = " #2461BF " ForeColor = " White " HorizontalAlign = " Center " />
43 < SelectedRowStyle BackColor = " #D1DDF1 " Font - Bold = " True " ForeColor = " #333333 " />
44 < HeaderStyle BackColor = " #507CD1 " Font - Bold = " True " ForeColor = " White "
45 Height = " 30px " />
46 < EditRowStyle BackColor = " #2461BF " />
47 < AlternatingRowStyle BackColor = " White " />
48 </ asp:GridView >
49  

后台代码:

代码
   
     
1 protected void NianDaiGridView_RowUpdating( object sender, GridViewUpdateEventArgs e)
2 {
3 // 获得编号 这个编号要在GridView中设置一下属性 DataKeyNames 这里设置的是数据库的主键ID
4 // e.RowIndex 这个通过 事件源 e 来获得RowIndex 代表 行号
5 int agentId = int .Parse( this .NianDaiGridView.DataKeys[e.RowIndex].Value.ToString());
6
7 // 这里是取得编辑时的值
8 // 通过 Rows[e.RowIndex] 当时事件的行
9 // FindControl[""]代表获取GridView内的控件名称,如FindControl("txtshehui")
10 // 我们取出来的控件的类型都是Control类型 的,所以,我们要根据我们定义的控件类型去转换,这里是TextBox类型 (注意 括号的加法)
11 // 如果是其它类型,就转换成其它的类型就可以了如DropDownList
12 TextBox txtguoming = (TextBox)NianDaiGridView.Rows[e.RowIndex].FindControl( " txtguoming " );
13 TextBox txtqizhi = (TextBox)NianDaiGridView.Rows[e.RowIndex].FindControl( " txtqizhi " );
14 string guoming = txtguoming.Text;
15 string qizhi = txtqizhi.Text;
16 // 调用修改的方法
17
18 // 实例化对象
19 txpj.Model. *** model = new txpj.Model. *** ();
20 model.niandaid = agentId;
21 model.guoming = guoming;
22 model.qizhi = qizhi;
23 txpj.BLL. *** bll = new txpj.BLL. *** ();
24 bll.Update(model);
25
26 Page.ClientScript.RegisterStartupScript( this .GetType(), " PopupScript " , " alert('修改成功!') " , true );
27
28 // 修改完全后,把EditIndex设置为-1 ,非编辑状态
29 this .NianDaiGridView.EditIndex = - 1 ;
30
31 // 记得要重新调用一下绑定数据的方法哦
32 dataBind();
33 }

 

你可能感兴趣的:(GridView)