2013-09-23 37 views
0

您好編碼器我有一個搜索用戶頁面,我有一個texbox,按鈕和一個gridview。現在,當我輸入用戶的名稱,然後單擊搜索按鈕時,我可以看到用戶的詳細信息。如何讓用戶在gridview中選擇複選框時出現錯誤

我的數據庫是這樣

enter image description here

我已經綁定我在頁面加載的GridView,這樣我還可以查看用戶的詳細信息,在GridView控件這樣的:

enter image description here

現在在這個gridview中,你可以看到一個IsEnable頭,它有複選框控件。該啓用有點在創建用戶時始終爲真。

我想要的是,當用戶點擊該複選框,該位變成假我在rowdatabound上試試這個,但它給我的對象引用錯誤,你可以告訴我當用戶選擇該複選框時,我會做什麼使我的位錯誤

請幫助我,這得益於對提前

現在,這是我現在用我的代碼做現在告訴我如何更新我能夠從真有點假的行更新

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     this.GetGridData(); 
    } 
} 
protected void BindGrid() 
{ 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("Select * from CreateUser", con); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 
    con.Close(); 
    if (ds.Tables[0].Rows.Count > 0) 
    { 
     GridView1.DataSource = ds; 
     GridView1.DataBind(); 
    } 
    else 
    { 
     ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); 
     GridView1.DataSource = ds; 
     GridView1.DataBind(); 
     int columncount = GridView1.Rows[0].Cells.Count; 
     GridView1.Rows[0].Cells.Clear(); 
     GridView1.Rows[0].Cells.Add(new TableCell()); 
     GridView1.Rows[0].Cells[0].ColumnSpan = columncount; 
     GridView1.Rows[0].Cells[0].Text = "No Records Found"; 
    } 
} 
private void GetGridData() 
{ 
    con.Open(); 
    string query = "Select * from CreateUser"; 
    da = new SqlDataAdapter(query, con); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 
    GridView1.DataSource = ds; 
    GridView1.DataBind(); 
    con.Close(); 
} 
protected void btnSearchUser_Click(object sender, EventArgs e) 
{ 
    this.BindGrid(); 
} 
protected void lnkdelete_Click(object sender, EventArgs e) 
{ 
    LinkButton lnkbtn = sender as LinkButton; 
    //getting particular row linkbutton 
    GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow; 
    //getting userid of particular row 
    int UserID = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString()); 
    string FirstName = gvrow.Cells[0].Text; 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("delete from CreateUser where UserID=" + UserID, con); 
    int result = cmd.ExecuteNonQuery(); 
    con.Close(); 
    if (result == 1) 
    { 
     BindGrid(); 
     //Displaying alert message after successfully deletion of user 
     ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + FirstName + " details deleted successfully')", true); 
     this.GetGridData(); 
    } 
} 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //if (e.Row.RowType == DataControlRowType.DataRow) 
    //{ 
    // //getting username from particular row 
    // string FirstName = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "FirstName")); 
    // //identifying the control in gridview 
    // LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete"); 
    // //raising javascript confirmationbox whenver user clicks on link button 
    // lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox('" + FirstName + "')"); 
    //} 
} 
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    GridView1.PageIndex = e.NewPageIndex; 
    GetGridData(); 
} 
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
{ 
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; 
    Label userid= (Label)row.FindControl("lblUserID"); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("delete FROM CreateUser where UserID='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
    BindGrid(); 
} 
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    GridView1.EditIndex = e.NewEditIndex; 
    BindGrid(); 
} 
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e) 
{ 
    string query = string.Empty; 
    string userid = GridView1.DataKeys[e.RowIndex].Values["UserID"].ToString(); 
    //Label id = GridView1.Rows[e.RowIndex].FindControl("lblUserID") as Label; 
    TextBox FirstName = GridView1.Rows[e.RowIndex].FindControl("txtFirstName") as TextBox; 
    TextBox LastName = GridView1.Rows[e.RowIndex].FindControl("txtLastName") as TextBox; 
    TextBox DomainID = GridView1.Rows[e.RowIndex].FindControl("txtDomainID") as TextBox; 
    TextBox EmailID = GridView1.Rows[e.RowIndex].FindControl("txtEmailID") as TextBox; 
    TextBox Password = GridView1.Rows[e.RowIndex].FindControl("txtPassword") as TextBox; 
    TextBox ConfirmPassword = GridView1.Rows[e.RowIndex].FindControl("txtConfirmPassword") as TextBox; 
    TextBox RoleType = GridView1.Rows[e.RowIndex].FindControl("txtRoleType") as TextBox; 
    CheckBox IsEnable = GridView1.Rows[e.RowIndex].FindControl("chkIsEnableEdit") as CheckBox; 
    //TextBox textadd = (TextBox)row.FindControl("txtadd"); 
    //TextBox textc = (TextBox)row.FindControl("txtc"); 
    GridView1.EditIndex = -1; 
    con.Open(); 
    //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn); 
    SqlCommand cmd = new SqlCommand("update CreateUser set FirstName='" + FirstName.Text + "',LastName='" + LastName.Text + "',DomainID='" + DomainID.Text + "',EmailID='" + EmailID.Text + "',Password='" + Password.Text + "',ConfirmPassword='" + ConfirmPassword.Text + "',RoleType='" + RoleType.Text + "',Enable='" + IsEnable.Checked + "' where UserID='" + userid + "'", con); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
    BindGrid(); 
    //GridView1.DataBind(); 
} 
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
{ 
    GridView1.EditIndex = -1; 
    BindGrid(); 
} 

}

+3

什麼線是對象引用錯誤嗎?順便說一句,如果(chk.Checked && chk == null)'沒有任何意義。 –

+0

sry我沒有更新,它只有如果(chk.checked)不是如果(chk.checked && chk == null) – amitesh

+0

爲什麼你要通過網格的原料,但然後檢查e.Row? – user844541

回答

1

把一些防禦性的編碼在第一位,並找出對象引用錯誤發生:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     var chk = (CheckBox)e.Row.FindControl("chkEnable"); 
     if (chk == null || !chk.checked) return; 
     var userId =GridView1.DataKeys[row.RowIndex].Value as string; 
     if (string.IsNullOrWhiteSpace(userId)) return; 
     var query = "update CreateUser set Enable='False' where UserID='" + userid + "'"; 
     cmd = new SqlCommand(query, con); 
     cmd.CommandType = CommandType.Text; 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     } 
} 

可以使用CheckChanged事件:

<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:CheckBox ID="chkEnable" runat="server" AutoPostBack="true" OnCheckedChanged="ChkEnable_CheckedChanged" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

,然後更新CheckChanged事件數據庫:

protected void ChkEnable_CheckedChanged(object sender, EventArgs e) 
{ 
    int selRowIndex = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex; 
    var userId = gridView.DataKeys[selRowIndex].Value as string; 
    if (string.IsNullOrWhiteSpace(userId)) return; 
    var checkBox = gridView.Rows[selRowIndex].FindControl("chkEnable") as CheckBox; 
    if (checkBox == null) return; 
    var query = "update CreateUser set Enable= @Enabled where UserID='" + userid + "'"; 
    cmd = new SqlCommand(query, con); 
    cmd.CommandType = CommandType.Text; 
    cmd.Parameters.AddWithValue("Enabled", checkBox.Checked); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 

} 
+0

你的代碼有錯誤隊友檢查它 – amitesh

+0

你的代碼是好的,但是當我檢查複選框,我想讓這個啓用位爲假,這是不是很好 – amitesh

+0

如何能複選框工作事件,如果我點擊複選框然後我的更新查詢觸發並更改到數據庫 – amitesh

0

首先你爲什麼要這麼做RowDataBound事件? RowDataBound發生在一行爲 時與gridview綁定,但這裏我們必須更新gridview行。 如果您必須更新行,您可以使用RowUpdating命令執行。

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
     {  

      Checkbox chkbx = ((Checkbox)(row.Cells[8].Controls[0])); 
      bool IsChecked =chkbx.Checked ;   
      //You can write your update command here. 
     } 



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
      GridViewRow row = GridView1.Rows[e.RowIndex]; 
     if (e.Row.RowType == DataControlRowType.DataRow) 
      { 

      TextBox MyName= (TextBox)e.Row.FindControl("FirstName"); 

      string name = Myname.text; 
      //For checkbox 
      Checkbox chkbx = (Checkbox)e.Row.FindControl("IsEnabled"); 

      if(chkbx.Checked ==true) 
      { 
       // do your stuff for checkbox checked. 
      } 
      else 
      { 
       // do your stuff if checkbox is not selected 
       } 
      } 
    } 
+0

我必須檢查使用行數據綁定選擇哪個ID,以便我可以在數據庫中進行更改 – amitesh

+0

如果點擊複選框,然後我的更新,如何點擊複選框事件工作查詢火災並將其更改爲數據庫 – amitesh

+0

查看「checkbox」每次點擊更改數據庫值是個壞主意,我建議您創建一個按鈕或其他可以更新已經勾選或取消選中的行的按鈕。 –

0

最後我得到了我的回答感謝您的回覆傢伙

protected void Page_Load(object sender, EventArgs e) 
{ 
if (!Page.IsPostBack) 
{ 
    this.GetGridData(); 
} 
} 
protected void BindGrid() 
{ 
con.Open(); 
SqlCommand cmd = new SqlCommand("Select * from CreateUser", con); 
SqlDataAdapter da = new SqlDataAdapter(cmd); 
DataSet ds = new DataSet(); 
da.Fill(ds); 
con.Close(); 
if (ds.Tables[0].Rows.Count > 0) 
{ 
    GridView1.DataSource = ds; 
    GridView1.DataBind(); 
} 
else 
{ 
    ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); 
    GridView1.DataSource = ds; 
    GridView1.DataBind(); 
    int columncount = GridView1.Rows[0].Cells.Count; 
    GridView1.Rows[0].Cells.Clear(); 
    GridView1.Rows[0].Cells.Add(new TableCell()); 
    GridView1.Rows[0].Cells[0].ColumnSpan = columncount; 
    GridView1.Rows[0].Cells[0].Text = "No Records Found"; 
} 
} 
    private void GetGridData() 
    { 
    con.Open(); 
    string query = "Select * from CreateUser"; 
    da = new SqlDataAdapter(query, con); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 
    GridView1.DataSource = ds; 
    GridView1.DataBind(); 
    con.Close(); 
    } 
protected void btnSearchUser_Click(object sender, EventArgs e) 
{ 
    this.BindGrid(); 
} 
protected void lnkdelete_Click(object sender, EventArgs e) 
{ 
    LinkButton lnkbtn = sender as LinkButton; 
    //getting particular row linkbutton 
    GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow; 
    //getting userid of particular row 
    int UserID = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString()); 
    string FirstName = gvrow.Cells[0].Text; 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("delete from CreateUser where UserID=" + UserID, con); 
    int result = cmd.ExecuteNonQuery(); 
    con.Close(); 
    if (result == 1) 
    { 
     BindGrid(); 
     //Displaying alert message after successfully deletion of user 
     ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + FirstName + " details deleted successfully')", true); 
     this.GetGridData(); 
    } 
} 


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
GridView1.PageIndex = e.NewPageIndex; 
GetGridData(); 
} 
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
{ 
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; 
Label userid= (Label)row.FindControl("lblUserID"); 
con.Open(); 
SqlCommand cmd = new SqlCommand("delete FROM CreateUser where UserID='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con); 
cmd.ExecuteNonQuery(); 
con.Close(); 
BindGrid(); 
} 
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    GridView1.EditIndex = e.NewEditIndex; 
    BindGrid(); 
} 
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e) 
{ 
string query = string.Empty; 
string userid = GridView1.DataKeys[e.RowIndex].Values["UserID"].ToString(); 
//Label id = GridView1.Rows[e.RowIndex].FindControl("lblUserID") as Label; 
TextBox FirstName = GridView1.Rows[e.RowIndex].FindControl("txtFirstName") as TextBox; 
TextBox LastName = GridView1.Rows[e.RowIndex].FindControl("txtLastName") as TextBox; 
TextBox DomainID = GridView1.Rows[e.RowIndex].FindControl("txtDomainID") as TextBox; 
TextBox EmailID = GridView1.Rows[e.RowIndex].FindControl("txtEmailID") as TextBox; 
TextBox Password = GridView1.Rows[e.RowIndex].FindControl("txtPassword") as TextBox; 
TextBox ConfirmPassword = GridView1.Rows[e.RowIndex].FindControl("txtConfirmPassword") as TextBox; 
TextBox RoleType = GridView1.Rows[e.RowIndex].FindControl("txtRoleType") as TextBox; 
CheckBox IsEnable = GridView1.Rows[e.RowIndex].FindControl("chkIsEnableEdit") as CheckBox; 
//TextBox textadd = (TextBox)row.FindControl("txtadd"); 
//TextBox textc = (TextBox)row.FindControl("txtc"); 
GridView1.EditIndex = -1; 
con.Open(); 
//SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn); 
SqlCommand cmd = new SqlCommand("update CreateUser set FirstName='" + FirstName.Text + "',LastName='" + LastName.Text + "',DomainID='" + DomainID.Text + "',EmailID='" + EmailID.Text + "',Password='" + Password.Text + "',ConfirmPassword='" + ConfirmPassword.Text + "',RoleType='" + RoleType.Text + "',Enable='" + IsEnable.Checked + "' where UserID='" + userid + "'", con); 
cmd.ExecuteNonQuery(); 
con.Close(); 
BindGrid(); 
//GridView1.DataBind(); 
} 
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
{ 
GridView1.EditIndex = -1; 
BindGrid(); 
} 
} 
+0

好,所以你在GridView1_RowUpdating1命令中這樣做,這就是我問的問題,因爲你問的是RowDataBound事件。 –

+0

還有一件事情使用參數化查詢,您的查詢很容易出現'SqlInjection'。還有一件事,你一次更新一行,那麼你可以更容易地使用'RowCommand'事件來做到這一點。 –

相關問題