asp.net gridview checkbox selection

蹲街弑〆低调 提交于 2019-12-30 01:22:05

问题


Need some help to solve this.

I have a gridview and inside the gridview I have a checkbox and after clicking the checkbox, I am doing a postback event and trying to update this particular row only on the database.

This is my gridview checkbox code. see the OnCheckedChanged.

<asp:TemplateField HeaderText="Sample">
  <ItemTemplate>
     <asp:CheckBox runat="server" 
                   ID="chkSample" 
                   Checked='<%# Bind("Sample") %>' 
                   OnCheckedChanged="UpdateSupplyLed" 
                   AutoPostBack="True">
    </asp:CheckBox> 
  </ItemTemplate>
</asp:TemplateField>

Code:

protected void UpdateSupplyLed(object sender, EventArgs e)
{
    foreach (GridViewRow di in SamplingGridView.Rows)    
    {        
        CheckBox chkBx = (CheckBox)di.FindControl("chkSample");
        if (chkBx != null && chkBx.Checked)
        {
            //update database logic here.
        }
    }
}

The above code works fine but it is getting me all the checkboxes that are checked irresepective of the one that I just checked. I don't want all of them.

How can I get the only one row value that have been just checked. Some of the rows might have been checked already because the status is true for those records and I don't want to update those records.

I think I've got my question right!

Update: The answer is:

protected void UpdateSupplyLed(object sender, EventArgs e)
{
    CheckBox chkSampleStatus = sender as CheckBox;        
    bool sample = chkSampleStatus.Checked;            
    GridViewRow row = chkSampleStatus.NamingContainer as GridViewRow;        
    TextBox txtId = row.FindControl("Id") as TextBox;            
    int id = Int32.Parse(txtId.Text);
}

回答1:


Try this:

CheckBox chkBx = sender as CheckBox;

Rather than iterate all the rows.

I haven't used CheckBox's in a GridView in this way myself. Usually I would use the GridView's OnRowCommand event instead and use the RowIndex or CommandArgument value to update the database.

Thinking about it OnRowcommand could be tricky for a CheckBox to fire, a better solution might be sticking with the CheckChanged event of the checkbox and navigate up to the GridViewRow serverside using controls NamingContainer. Something like:

GridViewRow row = chkBx.NamingContainer as GridViewRow;

I'm assuming the goes CheckBox => Cell => Row if you Google ASP.NET NamingContainer you'll get some more specifics.




回答2:


Try this also if above not working exactly

DataTable dtGetStTable = (DataTable)ViewState["dtNewStudents"];

//Looping throgh Grid and deleting row after finding the checked row
for (int i = dtGetStTable.Rows.Count - 1; i > -1; i--)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;

if (isChecked)
{
    try
    {
        string RollNo = dtGetStTable.Rows[i]["RollNo"].ToString();
        string Class = dtGetStTable.Rows[i]["Class"].ToString();
        string Division = dtGetStTable.Rows[i]["Division"].ToString();
        //Deleting the Reocrd from DataBase
        TotalRecordDeleted += objSRE.DeleteTheSelectedRecord(RollNo, Class, Division);

        dtGetStTable.Rows[i].Delete();
        dtGetStTable.AcceptChanges();
    }
    catch (Exception ex)
    {

        //throw;
    }
}

}



来源:https://stackoverflow.com/questions/1340901/asp-net-gridview-checkbox-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!