skip to Main Content

I have created an application aps.net c# within VS2019

I have a grid view, which has a checkbox in the column header.
Once a user has clicked on a seperate button to display, I would like to validate the checkbox called "chkALL" thats within the column header to see if it is checked or not. I cant seems to get the right syntax to do this.
Any advise please?

My ASPX code is here:
<asp:TemplateField HeaderText="Chg">

<asp:CheckBox ID="ChkAll" runat="server" AutoPostBack="true" OnCheckedChanged="ChkAll_CheckedChanged" />

<asp:CheckBox ID="ChkRow" runat="server" AutoPostBack="true" OnCheckedChanged="ChkRow_CheckedChanged" RowIndex="<%# Container.DataItemIndex %>" />

</asp:TemplateField>

This is the c# code I have tried

CheckBox chkbox = (CheckBox)tgvCorrelations.Column[13].FindControl("chkAll");

2

Answers


  1. **You need to create function for use it when fire event change checked box,
    
    So this void it write** 
    
    
    
    
    
     private void  ChkAll_CheckedChanged(object sender, EventArgs e)
        {
        
            for (int i = 0; i < tgvCorrelations.Rows.Count; i++)  
                {  
                    CheckBox chk = 
                     (CheckBox)tgvCorrelations.Rows[i].Cells[0].FindControl("chkAll");  
                    if (chk.Checked)  
                    {
        
                    }
                    else
                    {
                        //...
                    }
                }
        }
    
    Login or Signup to reply.
  2. To get/use and enjoy the value of the check box in the heading?

    You can use find control against the GridView.HeaderRow.

    So, assuming this markup:

    <asp:Button ID="cmdLoad" runat="server" Text="Load Data"
        CssClass="btn"
        OnClick="cmdLoad_Click"
        />
    <br />
    <br />
    
    <asp:GridView ID="GVHotels" runat="server" AutoGenerateColumns="False"
        DataKeyNames="ID" CssClass="table" 
        Width="45%"
        ShowHeaderWhenEmpty="true"
        >
        <Columns>
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" />
            <asp:BoundField DataField="City" HeaderText="City" />
            <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
            <asp:BoundField DataField="Description" HeaderText="Description" />
            <asp:TemplateField 
                ItemStyle-HorizontalAlign="Center"
                HeaderStyle-HorizontalAlign="Center"
                >
                <ItemTemplate>
                    <asp:CheckBox ID="chkSel" runat="server" 
                        OnCheckedChanged="chkSel_CheckedChanged"
                        AutoPostBack="true"
                        />
                </ItemTemplate>
                <HeaderTemplate>
                    <div style="text-align:center">
                        Select All<br />
                    <asp:CheckBox ID="chkSelAll" runat="server"
                        OnCheckedChanged="chkSelAll_CheckedChanged"
                        AutoPostBack="true"
                        />
                    </div>
                </HeaderTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    So, when we hit the button to load the GridView, we then will get the value of the check box in the heading, and set all rows to checked (or un-checked).

    So, the code to load our grid looks like this:

        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void cmdLoad_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }
    
        void LoadGrid()
        {
    
            CheckBox chkSelAll = new CheckBox();
            chkSelAll.Checked = false;
            bool HoldCheck = false;
            if (GVHotels.Rows.Count > 0)
            {
                chkSelAll = (CheckBox)GVHotels.HeaderRow.FindControl("chkSelAll");
                HoldCheck = chkSelAll.Checked; 
            }
    
            string strSQL = @"SELECT * FROM tblHotelsA
                              ORDER BY HotelName";
    
            GVHotels.DataSource = General.MyRst(strSQL);
            GVHotels.DataBind();
    
            chkSelAll = (CheckBox)GVHotels.HeaderRow.FindControl("chkSelAll");
            chkSelAll.Checked = HoldCheck;  
    
            CheckAll(chkSelAll.Checked);
        }
    
        protected void chkSel_CheckedChanged(object sender, EventArgs e)
        {
    
        }
    
        protected void chkSelAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkSelAll = (CheckBox)sender;
            CheckAll(chkSelAll.Checked);
        }
    
        void CheckAll(bool chk)
        {
            // check/un check all check box for each row
    
            foreach (GridViewRow gRow in GVHotels.Rows)
            {
                CheckBox chkSel = (CheckBox)gRow.FindControl("chkSel");
                chkSel.Checked = chk;
            }
        }
    

    Note VERY close, that WHEN we re-bind the GridView, then the setting of the check box is lost. So, if we are to re-bind the GridView, but ALSO respect the check box, then we get/grab/save the check box, re-load grid, and then re-set the check box back into the heading. (the value is LOST when we re-bind, so above code takes this into account).

    Note that after the grid loads, one might still want to check all (or un-check all) by clicking on the header. That code is somewhat easier since we can use "sender" to get the check box in question.

    So, that code is:

        protected void chkSelAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkSelAll = (CheckBox)sender;
            CheckAll(chkSelAll.Checked);
        }
    

    And the result is thus this:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search