views:

3519

answers:

2

Guys,

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>



 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!

Cheers, Narendra

Edited.

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  A: 

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 NameingContainer. 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.

HollyStyles
Thank you for your answer. But I need some other values from that same row so how can I get the row Id of that particular gridview row. so that I can get Id of the record.
Supremestar
Then I recommend not binding the checkbox's OnCheckChanged, use the GridViews OnRowCommand instead, the row is supplied in the GridVieweCommandEventArgs
HollyStyles
HollyStyles,I don't have a select command field in the Gridview so Gridview row command is not firing. What should I do my friend?Please help.
Supremestar
Made some edits. Yuo could attach a selectedIndexChanged to the CheckBox but as I was writing it I thought that really smells as a solution.
HollyStyles
Man you are a star, really you are! It is working like a charm with slight modification i.e GridViewRow row = chkBx.NamingContainer as GridViewRow;Thanking you for this.How can I mark your comments as answer.Cheers
Supremestar
@Supremester: No problem. Accepting the answer accepts all comments also. I've Edited my answer to match your edits.
HollyStyles
A: 

Thanks buddy. it helped...:)

Gagan