Tim
Tim

Reputation: 1239

looping through gridview does not always work

Not sure what is causing this I have tried a few suggestions none seem to help. I have debugged my application dozens of times and the problem never occur while debugging everything steps through just fine. But when I publish my application and allow people to use it is when the problem occurs and it is not for everyone just randomly decides that a checkbox is not checked and skips the whole process also on the front end I have a validation that requires at least one checkbox to checked before the button_click will fire so I know that they had to have one checked.

Gridview

 <div id="divEventDetail">
    <asp:GridView ID="grdEventDetail" runat="server" AutoGenerateColumns="False" DataKeyNames="EDID" Width="381px" OnRowDataBound="grdEventDetail_RowDataBound" GridLines="Horizontal">
    <Columns>
  <asp:TemplateField HeaderText="EventID" Visible="False">
           <ItemTemplate>
     <asp:Label ID="lblEventID" runat="server" Text='<%#     Eval("EDID") %>'></asp:Label>
           </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Register" ItemStyle-CssClass="template-center">
           <ItemTemplate >                         
               <asp:CheckBox ID="chkRegister" runat="server"/>
           </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Wait List" ItemStyle-CssClass="template-center">
           <ItemTemplate> 
               <asp:CheckBox ID="chkWaitList" runat="server" />   
           </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </div>

CodeBehind

 protected void registerEvent()
        {
            foreach (GridViewRow row in grdEventDetail.Rows)
            {

                CheckBox chkR = row.FindControl("chkRegister") as CheckBox;
                CheckBox chkW = row.FindControl("chkWaitList") as CheckBox;

                if (chkR != null && chkW != null)// It is a datarow
                {

                    GridViewRow Rowr = ((GridViewRow)chkR.Parent.Parent);
                    GridViewRow Roww = ((GridViewRow)chkW.Parent.Parent);

                    if ((chkR.Checked) || (chkW.Checked))
                    // if ((((CheckBox)row.FindControl("chkRegister")).Checked == true) || (((CheckBox)row.FindControl("chkWaitList")).Checked == true))
                    {
                        Label eventID = row.FindControl("lblEventID") as Label;
***Then i do my database stuff here

Upvotes: 1

Views: 565

Answers (1)

afzalulh
afzalulh

Reputation: 7943

I believe the grdEventDetail GridView doesn't have CheckBoxes in each row. For example, HeaderRow and FooterRow probably don't have those CheckBoxes.

I would rewrite the code to eliminate any error:

protected void registerEvent()
{
    foreach (GridViewRow row in grdEventDetail.Rows)
    {
        CheckBox chkR = row.FindControl("chkRegister") as CheckBox;
        CheckBox chkW = row.FindControl("chkWaitList") as CheckBox;

        if(chkR != null && chkW != null)// It is a datarow
        {
            GridViewRow Rowr = ((GridViewRow)chkR.Parent.Parent);
            GridViewRow Roww = ((GridViewRow)chkW.Parent.Parent);

            if ((chkR.Checked) || (chkW.Checked))  
            {
                //Your code goes here
            }
        }
    }
}

Upvotes: 4

Related Questions