Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having checkbox in Gridview Header , if i will check header checkbox it will check all the rows checkboxes and one more option when user submit the form data it should required atleast one checkbox checked.without using javascript function...only c# and asp.net code... plz help..
Posted
Updated 29-Aug-13 2:33am
v2

Please refer to this very basic code. I assume you have checkbox at first cell of gridview ( Cells[0] )
C#
int count=0;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
     CheckBox chk= (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
     if (chk.Checked == true)
     {
       count++;
     }
}
if(count==0)
{
//no check box is checked take action accordingly
}
else
{
//Atleast one checkbox is checked proceed with the submit procedure
}

Regards..:)
 
Share this answer
 
v2
Hi

You also do this finding the header template and based on the status of the header template checkbox you can update the other status in the item template checkbox.

C#
protected void cbxHdrPresent_OnCheckedChanged(object sender, EventArgs e)
    {
        int chkCount = 0;
        CheckBox chkHead = (CheckBox)gvBrowse.HeaderRow.FindControl("cbxHdrPresent");
        if (chkHead.Checked)
        {
            for (int i = 0; i < gvBrowse.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvBrowse.Rows[i].FindControl("cbxItmPresent");
                chk.Checked = true;
                chkCount++;
            }
        }
        else
        {
            for (int i = 0; i < gvBrowse.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvBrowse.Rows[i].FindControl("cbxItmPresent");
                chk.Checked = false;
            }
        }

        if (chkCount > 0)
        {

        }
        else
        {

        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900