使用Commandfiled按钮更新模板字段gridview中的行?

时间:2014-02-09 18:21:53

标签: c# asp.net .net gridview updating

这里的代码有什么问题...我正在尝试通过编辑,更新,取消命令字段按钮来更新网格视图中一行中获取的值。

     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    Label lblid = (Label)GridView1.Rows[e.RowIndex].FindControl("Elblid");
    TextBox txtuser = (TextBox)GridView1.Rows[e.RowIndex].FindControl("EtxtUserName");
    TextBox txtpass = (TextBox)GridView1.Rows[e.RowIndex].FindControl("EPassword");

    if (lblid.Text != "" || lblid.Text != null)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("Update Login set UserName='" + txtuser.Text + "',Password='" + txtpass.Text + "' Where Id=" + lblid.Text, con);
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        FillGrid();
    }

这是抛出的错误:enter image description here

PS:在页面指令中将事件验证设置为false。这个错误消失了,但代码没有做任何意图。

1 个答案:

答案 0 :(得分:0)

它显示的是什么错误?

试试这个

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    Label lblid = (Label)row.FindControl("Elblid");
    TextBox txtuser = (TextBox)row.FindControl("EtxtUserName");
    TextBox txtpass = (TextBox)row.FindControl("EPassword");
}