locked
Unable to consistently get checked rows in GridView when in UpdatePanel RRS feed

  • Question

  • User-1033004070 posted

    I have a GridView located in an UpdatePanel on my webpage.  To take actions against checked rows in the GridView, I have a method that first determines if at least one row is checked.  Sometimes it works and sometimes it doesn't.  Typically the first time I check any number of rows, it returns false.  Then I recheck the rows again and it returns true and the actions begin.  After that is complete, I call GridView.Databind() to refresh the data.  At that time the evolution begins again, I have to check the rows twice for them to be captured in code as True.

    int i = 0;
                foreach (GridViewRow row in GridView3.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        CheckBox check = row.FindControl("CheckBox1") as CheckBox;
                        if (check.Checked)
                        {
                            i++;
                        }
                    }
                }
                
                if (i != 0)
                {
                   //do something
                }

    Wednesday, October 8, 2014 11:58 AM

Answers

  • User61956409 posted

    Hi sz56ms01,

    Thanks for your post.

    According to your description, I create a sample for your reference.

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="up1" runat="server">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnChildCheckedChanged" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Item" HeaderText="Item" />
                        <asp:BoundField DataField="Price" HeaderText="Price" />
                    </Columns>
                </asp:GridView>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnchk" EventName="Click"/>
            </Triggers>
        </asp:UpdatePanel>
        <asp:Button ID="btnchk" runat="server" Text="Check" OnClick="btnchk_Click" />
    </div> 
    
    protected void OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (sender as CheckBox);
        GridView gv = chk.NamingContainer.Parent.Parent as GridView;
        foreach (GridViewRow row in gv.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                (row.FindControl("CheckBox1") as CheckBox).Checked = chk.Checked;
            }
        }
    }
    protected void OnChildCheckedChanged(object sender, EventArgs e)
    {
        //do something
    }
    
    protected void btnchk_Click(object sender, EventArgs e)
    {
        bool chked = false;
        int chknumber = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                chked = (row.FindControl("CheckBox1") as CheckBox).Checked;
                if (chked)
                {
                    chknumber++;
                } 
            }
        }
    
        if (chknumber>0)
        {
            //least one row is checked
            //do something
        }
    } 
    

    If you have any question about this solution, please let me know freely.

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 9, 2014 1:44 AM

All replies

  • User-1033004070 posted

    Sorry, I should have posted the html code also...

    <asp:UpdatePanel ID="pnlAssignDelivery" UpdateMode="Conditional" runat="server" >
                                    <ContentTemplate>
                                
                                        <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" CssClass="SmallGrid" DataKeyNames="recordID" DataSourceID="WSMdataDS" OnDataBound="GridView3_DataBound" OnRowDataBound="GridView3_RowDataBound">
                                            <Columns>
                                                <asp:TemplateField HeaderText="Select">
                                                    <HeaderTemplate>
                                                        <asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
                                                    </HeaderTemplate>
                                                    <ItemTemplate>
                                                        <asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
                                                    </ItemTemplate>
                                                    <ItemStyle HorizontalAlign="Center" />
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="recordID">
                                                    <EditItemTemplate>
                                                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("recordID") %>'></asp:Label>
                                                    </EditItemTemplate>
                                                    <ItemTemplate>
                                                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("recordID") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:BoundField DataField="deliveryMethod" HeaderText="Delivery Method" SortExpression="deliveryMethod" />
                                                <asp:BoundField DataField="dataImportMethod" HeaderText="Import Method" ReadOnly="True" SortExpression="dataImportMethod" />
                                                <asp:BoundField DataField="BO_Number" HeaderText="BO_Number" ReadOnly="True" SortExpression="BO_Number" Visible="False" />
                                                <asp:BoundField DataField="PIV" HeaderText="PIV" ReadOnly="True" SortExpression="PIV" Visible="False" />
                                                <asp:BoundField DataField="RNL" HeaderText="RNL" ReadOnly="True" SortExpression="RNL" />
                                                <asp:BoundField DataField="SeatType" HeaderText="Seat Type" ReadOnly="True" SortExpression="SeatType" />
                                                <asp:BoundField DataField="SeatClass" HeaderText="Seat Class" ReadOnly="True" SortExpression="SeatClass" />
                                                <asp:BoundField DataField="SeatPurpose" HeaderText="Seat Purpose" ReadOnly="True" SortExpression="SeatPurpose" />
                                                <asp:TemplateField HeaderText="Machine_Name" SortExpression="Machine_Name">
                                                    <EditItemTemplate>
                                                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Machine_Name") %>'></asp:TextBox>
                                                    </EditItemTemplate>
                                                    <ItemTemplate>
                                                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Machine_Name") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:BoundField DataField="Seat" HeaderText="Seat Id" ReadOnly="True" SortExpression="Seat" />
                                                <asp:BoundField DataField="Base_CLIN_Number" HeaderText="Base CLIN" ReadOnly="True" SortExpression="Base_CLIN_Number" />
                                                <asp:BoundField DataField="Additional_CLINs" HeaderText="Additional CLINs" ReadOnly="True" SortExpression="Additional_CLINs" />
                                                <asp:BoundField DataField="Base" HeaderText="Base" ReadOnly="True" SortExpression="Base" />
                                                <asp:BoundField DataField="UIC" HeaderText="UIC" ReadOnly="True" SortExpression="UIC" />
                                                <asp:BoundField DataField="Claimant" HeaderText="Claimant" ReadOnly="True" SortExpression="Claimant" />
                                            </Columns>
                                        </asp:GridView>
                                
                                  </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="btnAssignSD" EventName="Click" />
                                        <asp:AsyncPostBackTrigger ControlID="btnAssignSelfDeploy" EventName="Click" />
                                        <asp:AsyncPostBackTrigger ControlID="btnAssignFS" EventName="Click" />
                                    </Triggers>
                                </asp:UpdatePanel>

    Wednesday, October 8, 2014 12:03 PM
  • User61956409 posted

    Hi sz56ms01,

    Thanks for your post.

    According to your description, I create a sample for your reference.

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="up1" runat="server">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnChildCheckedChanged" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Item" HeaderText="Item" />
                        <asp:BoundField DataField="Price" HeaderText="Price" />
                    </Columns>
                </asp:GridView>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnchk" EventName="Click"/>
            </Triggers>
        </asp:UpdatePanel>
        <asp:Button ID="btnchk" runat="server" Text="Check" OnClick="btnchk_Click" />
    </div> 
    
    protected void OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (sender as CheckBox);
        GridView gv = chk.NamingContainer.Parent.Parent as GridView;
        foreach (GridViewRow row in gv.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                (row.FindControl("CheckBox1") as CheckBox).Checked = chk.Checked;
            }
        }
    }
    protected void OnChildCheckedChanged(object sender, EventArgs e)
    {
        //do something
    }
    
    protected void btnchk_Click(object sender, EventArgs e)
    {
        bool chked = false;
        int chknumber = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                chked = (row.FindControl("CheckBox1") as CheckBox).Checked;
                if (chked)
                {
                    chknumber++;
                } 
            }
        }
    
        if (chknumber>0)
        {
            //least one row is checked
            //do something
        }
    } 
    

    If you have any question about this solution, please let me know freely.

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 9, 2014 1:44 AM