注册 登录
编程论坛 ASP.NET技术论坛

求gridview中的更新代码

刘小芳 发布于 2006-10-19 22:44, 1327 次点击

小女子刚开始学编程,今日学习对在gridview中显示的数据进行修改,我点编辑后,所选数据变成文本框,我修改后,点更新按钮,请问更新按钮里面该写什么代码呢,我是学的C#,ASP.NET,等大家帮我忙哟

4 回复
#2
mylover6242006-10-19 23:31

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gvr = this.GridView1.Rows[e.RowIndex];
TextBox txt1 = (TextBox)gvr.Cells[1].Controls[0];
TextBox txt2 = (TextBox)gvr.Cells[2].Controls[0];
TextBox txt3 = (TextBox)gvr.Cells[3].Controls[0];

string UpdateStr = "update table set cell1='" + txt1.Text.Trim()
+ "',cell2='" + txt2.Text.Trim()
+ "',cell3='" + txt3.Text.Trim()
+ "' where id=" + this.GridView1.DataKeys[e.RowIndex].Value;
try
{
mycon.myExecuteNonQuery(UpdateStr); //自己写一个方法
Response.Write("<script language='javascript'>alert('修改成功!');</script>");
}
catch (Exception exp)
{
Response.Write("<script language='javascript'>alert('" + exp.Message + "');</script>");
}
finally
{
this.GridView1.EditIndex = -1;
BindData(); //自定义绑定
}
}

#3
lihongjun1982006-10-25 15:05
这个方法较简单    很好
#4
chenjincheng2013-03-21 08:36
这个方法是干什么用的呢?
#5
lizhenli12232014-05-11 10:20
为什么我用这个方法老说
无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。
1