2

I have GridView filled automatically as

<asp:GridView ID="gvValues" runat="server" 
    OnRowDataBound="gvValues_RowDataBound" 
    OnPageIndexChanging="gvValues_PageIndexChanging" 
    <Columns>           
        <asp:TemplateField HeaderText="#">
            <ItemTemplate>
                <%# gvValues.PageSize*gvValues.PageIndex+ Container.DisplayIndex+1  %> 
                <asp:CheckBox ID="chkProduct" runat="server" CssClass="chkProduct"/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField  HeaderText="online" meta:resourcekey="Online">
            <ItemTemplate > 
                <asp:CheckBox ID="chkProductonline" runat="server"  OnCheckedChanged ="chkProductonline_CheckedChanged" AutoPostBack="true"/>
            </ItemTemplate>
        </asp:TemplateField>

What I need is when I click on the chkProductonline checkbox, to fire an event and get the chkProductonline and chkProducton values. I have tried this but it always gives me null.

protected void chkProductonline_CheckedChanged(object sender, EventArgs e)
{
    var chkProductonline = FindControl("chkProductonline") as CheckBox;
    // bool ischeck = chkProductonline.Checked;

    var chkProduct = gvValues.FindControl("chkProduct") as CheckBox;
}

I can't loop the GridView. I need to do this one-by-one. Is there another way to do that?

0

2 Answers 2

1

You can try this:

protected void chkProductonline_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chkProductonline = sender as CheckBox;
    ...

    CheckBox chkProduct = chkProductionLine.NamingContainer.FindControl("chkProduct") as CheckBox;
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to call FindControl on a specific row. You will not be able to call it on the entire GridView because there exists repeating content (i.e. multiple chkProductionlines and chkProducts). A row knows of its checkboxes, and not of the others.

So what you can do is first get the CheckBox that called the event (your sender parameter, chkProductionline) and use its NamingContainer. Since it is contained in a GridView row, cast the row as such as use it to find the other controls you may need.

protected void chkProductonline_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chkProductionline = (CheckBox)sender;
    GridViewRow row = (GridViewRow)chkProductionline.NamingContainer;

    CheckBox chkProduct = (CheckBox)row.FindControl("chkProduct");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.