2014-02-09 63 views
0

這裏的代碼有什麼問題......我試圖通過編輯,更新,取消命令字段按鈕來更新網格視圖的一行中獲取的值。使用Commandfiled按鈕更新模板字段gridview中的行?

 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:設置當事件的驗證是在頁面指令虛假。這個錯誤消失了,但代碼沒有做到它的意圖。

回答

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"); 
}