问题描述
在DATAGRID中点了编辑按钮后,会把所有字段变成文本框,然后点更新就执行了方法,进行UPDATE这个时候,就是要把用户新填写的东西写入数据库,那么,取得用户输入的数据是关键,这里我用到了一个方法,如下privatevoiddg_estop_item_UpdateCommand(objectsource,System.Web.UI.WebControls.DataGridCommandEventArgse){TextBoxtb=(TextBox)(e.Item.Cells[1].Controls[0]);stringid=tb.Text.Trim();tb=(TextBox)e.Item.Cells[2].Controls[0];stringitem=tb.Text.Trim();略过下面执行的SQL方法}这里我得到了该字段的1列和2列,但是老大有需求,说必须用STRINGKEY的方式获得,也就是说e.Item.Cells[数字].Controls[数字]这个方法不可取,需要用类似e.Item.Cells[字符串].Controls[0]小弟是在无解,请达人帮忙,感激不尽老大有提示,类似如此LinkButtonl=(LinkButton)e.Item.Cells[9].FindControl("lbtn_del");但是,这个通过寻找控件ID的方法,我无法知道那几个TEXTBOX的ID呀,不知道要怎么做
解决方案
解决方案二:
求人帮忙呀感激不尽
解决方案三:
求人帮忙呀感激不尽
解决方案四:
protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse){stringteaid=Convert.ToString(this.GridView1.DataKeys[e.RowIndex].Value);stringteaname=(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]asTextBox).Text;stringteaage=(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]asTextBox).Text;using(SqlConnectioncon=newSqlConnection("server=.;uid=sa;pwd=;database=login1")){con.Open();stringstr="updateteachersetteaname='"+teaname+"',teaage='"+teaage+"'whereteaid='"+teaid+"'";SqlCommandcmd=newSqlCommand(str,con);cmd.ExecuteNonQuery();}this.GridView1.EditIndex=-1;this.bindGridView();}
以上是,cs<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"OnRowEditing="GridView1_RowEditing"OnRowUpdating="GridView1_RowUpdating"DataKeyNames="teaid"OnRowCancelingEdit="GridView1_RowCancelingEdit"><Columns><asp:BoundFieldDataField="teaid"HeaderText="编号"ReadOnly="true"/><asp:BoundFieldDataField="teaname"HeaderText="姓名"/><asp:BoundFieldDataField="teasex"HeaderText="性别"/><asp:BoundFieldDataField="teaage"HeaderText="年龄"/><asp:BoundFieldDataField="teaspe"HeaderText="专业"/><asp:BoundFieldDataField="teaaddress"HeaderText="地址"/><asp:CommandFieldShowEditButton="True"/></Columns></asp:GridView>
html
解决方案五:
protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse){stringteaid=Convert.ToString(this.GridView1.DataKeys[e.RowIndex].Value);stringteaname=(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]asTextBox).Text;stringteaage=(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]asTextBox).Text;using(SqlConnectioncon=newSqlConnection("server=.;uid=sa;pwd=;database=login1")){con.Open();stringstr="updateteachersetteaname='"+teaname+"',teaage='"+teaage+"'whereteaid='"+teaid+"'";SqlCommandcmd=newSqlCommand(str,con);cmd.ExecuteNonQuery();}this.GridView1.EditIndex=-1;this.bindGridView();}
以上是,cs<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"OnRowEditing="GridView1_RowEditing"OnRowUpdating="GridView1_RowUpdating"DataKeyNames="teaid"OnRowCancelingEdit="GridView1_RowCancelingEdit"><Columns><asp:BoundFieldDataField="teaid"HeaderText="编号"ReadOnly="true"/><asp:BoundFieldDataField="teaname"HeaderText="姓名"/><asp:BoundFieldDataField="teasex"HeaderText="性别"/><asp:BoundFieldDataField="teaage"HeaderText="年龄"/><asp:BoundFieldDataField="teaspe"HeaderText="专业"/><asp:BoundFieldDataField="teaaddress"HeaderText="地址"/><asp:CommandFieldShowEditButton="True"/></Columns></asp:GridView>
html
解决方案六:
是在2003里使用DATAGRID啊....stringteaname=(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]asTextBox).Text;这个取值方式被否决了CELLS[数字]不允许这么取值要我找其他方法
解决方案七:
求人帮忙呀感激不尽
解决方案八:
又学习了,朋友,不过我在做的时候,不是这样做,而是直接在datagrid中把要编辑的项直接用文本框显示。也就是说,在datagrid中在相应的列添加文本框,并在每行添加一个checkbox,这样,通过它选中后,再用代码中的findcontrol()方法,去判断是那个被选中,这样就能保存或更新那个值了。
解决方案九:
楼上的朋友请问相关代码能贴出来看看吗谢谢