Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to Check all Checkboxes without PostBack

Author: DevASP Team
Download Source Code :

In this article I will try to explain you how you can add javascript on Page_PreRender event. This will help full in scenarios where you don’t want to postback your page and page will work with server

To begin with this application, create a new application in 2.0 and drop grid view control on your page. Now add the following code on page load event:


        this.Page.PreRender +=new EventHandler(Page_PreRender); 


        SqlConnection objConn = new SqlConnection("Persist Security Info=False;User ID=uid;password=pwd;Initial Catalog=tempdatabase");

        SqlDataAdapter objAdap = new SqlDataAdapter();

        DataSet ds = new DataSet();




        objAdap = new SqlDataAdapter("Select RouteID from Route", objConn);


        GridView1.DataSource = ds;




In above code the main thing you will see is I have registered the Pre_Render event on page load now add the following code on your page:


protected void Page_PreRender(object sender, EventArgs e)


        string script;


        script = "<script language=\"javascript\" type=\"text/javascript\">";

        script += "function checkAll(chk){";

        script += "if(document.getElementById('" + GridView1.HeaderRow.FindControl("chkBox").ClientID + "').checked == chk.checked){";

        foreach (GridViewRow row in GridView1.Rows)


            script += "document.getElementById('" + row.FindControl("chkBox2").ClientID + "').checked = chk.checked;";           


        script += "}";

        script += "}";

        script += "</script>";



        ((CheckBox)GridView1.HeaderRow.FindControl("chkBox")).Attributes["onClick"] = "checkAll(this);";


        this.RegisterStartupScript("sample", script);


Here RegisterStartupScript will register your script on your browser page.   

Article Comments
This is a simple code to check all check boxes into Item templete.chkAll is used to chech all the chkFile check boxes.

protected void chkAll_CheckedChanged(object sender, EventArgs e)

int Rows = grdFileDetails .Rows.Count;
for (int j = 0; j < Rows; j++)
Control ctrl2 = grdFileDetails.Rows[j].Cells[0].FindControl("chkFile");
CheckBox chkBox2 = ((CheckBox)ctrl2);
if (i == 0)
chkBox2.Checked = true;
chkBox2.Checked = false;
if (i == 0)
i = 1;
i = 0;

Posted on 10/15/2007 5:26:56 AM by Shirish Bhale

well done !

Posted on 1/17/2008 7:51:23 AM by yy

Article about CheckBox

Posted on 3/12/2008 8:36:06 AM by uttam kumar singh

Add Article Comment:
Name :
Email Address :
Comments :
<< How to Add Rows and Cell to HTMLTable through Code

Disclaimer - Privacy
© 2002-2017