Checking All CheckBoxes in a GridView Using Client-Side Script

Hi;

I have  checkbox column in gridview and a checkbox in header gridview.

i write down code:

Dim checkbox As CheckBox = GridView2.HeaderRow.FindControl("HeaderCheckbox")

checkbox.Attributes("onclick") = "ChangeAllCheckBoxStates(this.checked);"

 

For Each row As GridViewRow In GridView2.Rows

Dim a As CheckBox = row.Cells(0).FindControl("CheckBox2")

If row.RowType = DataControlRowType.DataRow Then

ClientScript.RegisterArrayDeclaration("CheckboxIDs", String.Concat("'", a.ClientID, "'"))

End If

Next

 I run  my application and click on header checkbox but down error occur:

error:

Microsoft JScript runtime error: Object expected

what error occur?

Thanks,mohsen

0
mohsendeveloper
4/24/2008 2:37:22 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

4 Replies
800 Views

Similar Articles

[PageSpeed] 50

Hi

there is a lot of articles on that topic :

http://aspnet.4guysfromrolla.com/articles/053106-1.aspx

http://www.codeproject.com/KB/webforms/SelChkboxesDataGridView.aspx


Regards,

Anas Ghanem.


Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

0
anas
4/24/2008 6:52:22 PM

Hi anas,

There is nothing wrong with ur code. You have to do only few things.. First - You have to bind the javascrpt functions in RowDataBound event, In RowDataBound event u'll have to find the checkbox control and have to add javascript funciton.

like .. CheckBox checkBox=  ((CheckBox) e..... FindControl("chkBoxes")).Attribues.Add("onclick", "javascriptfunctionName");

Now ur javascript is bind for each CheckBox control, Ur javascript must be writen properly so it may not give any error.

This code will work fine.

In case of any problem or query, feel free to contact me.

Thank you'

Samra

0
rajiv
4/24/2008 7:05:06 PM

Watch the following step by step video to have a clear picture of the solution:

Selecting CheckBoxes Inside the GridView Control 


HighOnCoding
Wanna get high!
0
azamsharp
4/24/2008 7:14:13 PM
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="GridViewCheckbox.aspx.cs"
   Inherits="GridViewCheckbox" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
   <title>Untitled Page</title>

   <script type="text/javascript">

function
SetStatus(headObj) {

   var obj = document.getElementsByTagName("input");
     for(var i =0; i < obj.length ; i++) {  
        if(obj[i].type.toLowerCase() == "checkbox" &&
obj[i].name.toLowerCase() != headObj.name.toLowerCase() )
        {  
              if(headObj.checked)
                   obj[i].checked = true;                
              else
                   obj[i].checked = false;
         }
    }
    return true;
}

   </script>

</
head>
<
body>
   <form id="form1" runat="server">
       <div>
           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
               <Columns>
                   <asp:TemplateField>
                       <HeaderTemplate>
                           <asp:CheckBox runat="server" ID="HeaderLevelCheckBox"
OnClick="SetStatus(this);" />
                       </HeaderTemplate>
                       <ItemTemplate>
                           <asp:CheckBox runat="server" ID="RowLevelCheckBox" />
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:BoundField DataField="CompanyName" HeaderText="CompanyName"  />
                   <asp:BoundField DataField="Address" HeaderText="Address"  />
               </Columns>
           </asp:GridView>
       </div>
   </form>
</
body>
</
html>



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GridViewCheckbox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = c();
GridView1.DataBind();

}
public DataSet c()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Company");
DataRow dr;
dt.Columns.Add(new DataColumn("accountNo", typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyName", typeof(string)));
dt.Columns.Add(new DataColumn("Address", typeof(string)));
for (int i = 0; i <= 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Company" + i + Environment.NewLine + "Title" + i;
dr[2] = "Address" + i + Environment.NewLine + "Title" + i;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
return ds;
}
}


 

 


Santosh kumar
Mark as Answer on the post that helped you.



Blog
0
santosh
4/24/2008 11:28:06 PM
Reply:

Similar Artilces:

How to access dynamically Html controls (client side controls) using C# server side scripts
Folks,I have problem try to to access dynamically Html controls (client side controls) using C# server side scripts. Please give me helps.     The error is as follows Exception Details: System.InvalidCastException: Specified cast is not valid.Source Error: Line 79: { Line 80: Control c = Page.FindControl("ddlTC" + i); Line 81: ((DropDownList)c).DataSource = mdt; Line 82: ((DropDownList)c).DataTextField = "ClassID"; Line 83: ((DropDownList)c).DataValueField = "Course"; My aspx page source codes: ...<...

How to use client-side script(javascript) in DNA using .Net Remoting
Hi all,     It was known that SAO and Singleton object about .Net Remoting. When I Create an object in server side and hosting in IIS's virtual directory,I want to use javascript asynchronous invoking the remote object by HTTP channel, 80 port and SOAP formatter just like XMLHTTP invoking web service. My code as following, <script> objXmlDoc = new ActiveXObject("Msxml2.DOMDocument"); objHttp = new ActiveXObject("Msxml2.XMLHTTP"); strEnvelope = ?? objHttp.onreadystatechange = function() { ...... }   szUrl = "http://ServerNa...

Not able to un-check or check my checkbox control using Java Script with .aspx
Hello, I am trying to programatticaly un-check a checkbox on my web form using Java script. This is in an asp.net web form. Can someone spot what is wrong here? Below is the web form that can easily be cut and pasted into visual studio in order for you to see what is going on.   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Sampler._Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script...

Java script cannot differentiate client side controls in user control used multiple on same webform
I have a textbox with a automcomplete extender and a client side link that, when clicked, changes style.display from "none" to "" to display a div that contains a listview with all possible choices for the text box.  I am using javascript to change the div style.display when the link is clicked.  When I have two instances of the usercontrol on the same webform, both links display the div on the first control.  How can the client side javascript tell the difference between the two divs? Try putting the javascript of the user c...

checkbox client side onclick event to manage checked status of multiple checkbox controls or multiple items in a checkboxlist
I want to use javascript to run a client side function to manage the checked state of multiple checkbox controls.  For example, if checkboxA is checked by the user, I want to uncheck checkboxB & checkboxC and check checkboxD.  I do not want to make this a postback event. Also, I'm considering using a checkboxlist control.  If I were to use a checkboxlist control with 6 list items, how would I control the checked status of the different list items based upon an onclick event for one of the list items.  An example: if checkbox list item (1) is checked, I want to unc...

checking checkbox is checked or not using checkbox.checked
Hi,my checkbox is<asp:CheckBox ID="Chkcity" runat="server" Text="My own city"  OnCheckedChanged="EventHandler"/>code behind: Private Sub Chkcity_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Chkcity.CheckedChangedif (Chkcity ...............)end if End SubERROR    'EventHandler' is not a member of 'ASP.bpoaddjob_aspx'.    Please Mark the post as "ANSWER" if it helps you I think it should be like this:<asp:CheckBox ID="Chkcity"...

Using AJAX and client side scripting with .NET objects?
I'm wanting to use client-side scripting and AJAX with some drop down boxes.  I can't seem to do this with server-side .NET objects... changing the runat tag doesn't do anything either.  How can I expose .NET server objects so that they are affected by java script and AJAX? Depends on what, exactly, you're trying to do with them.  If you just need access to the html properties (e.g. selectedValue), then you simply grab them as if they were their html equivilants (select element) in javascript.  If you want to pass custom properties back and forth (or any .Net property ...

Showing or hiding controls using a client side script
This question might have ben asked number of times but my search did not yield a full answer to my question. I found some partial answers but I am sure a full well explained answer will satisfy me as a novice asp.net developer and people who are in the same boat as I am. I like to use a checkbox and based on the selection of the checkbox, I want to show or hide number of controls (mainly labels and text boxes but possible to include other controls in the future). I want to do that at the client side to avoid round-trip delays (hence I believe I have to run a client side java script). M...

How to use client-side script(javascript) in .Net Remoting
Hi all,     It was known that SAO and Singleton object about .Net Remoting. When I Create an object in server side and hosting in IIS's virtual directory,I want to use javascript asynchronous invoking the remote object by HTTP channel, 80 port and SOAP formatter just like XMLHTTP invoking web service. My code as following, <script>  objXmlDoc = new ActiveXObject("Msxml2.DOMDocument"); objHttp = new ActiveXObject("Msxml2.XMLHTTP"); strEnvelope = ?? objHttp.onreadystatechange = function() { ......  }    szUrl = &...

Hiding Group of Controls Using Client-Side Script
Hello Developers, I am trying to hide a group of controls enclosing them in a <div> element and altering its display property with client-side script. But, for any reason, it does not work and I just cannot figure out why. Any ideas? Complete example attached. Tomasz J<%@ Page Language="C#" %><%@ Register TagPrefix="ajaxToolkit" Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt...

Trying to use the FAQ " Sorting and paging in the GridView control when not using data source controls "Sorting and paging in the GridView control when not using..."
and I am not getting the results I had hoped for.  I keep getting a "Compiler Error Message: CS0123: No overload for 'AdhocJobGridView_PageIndexChanged' matches delegate 'System.EventHandler'".   Here is my event handling... protected void AdhocJobGridView_PageIndexChanged(object sender, GridViewPageEventArgs e){ try { AdhocJobGridView.DataSource = wsBaseInfoArray; AdhocJobGridView.PageIndex = e.NewPageIndex; AdhocJobGridView.DataBind(); }catch (Exception ex) {throw ex; }   Here is my aspx... <asp:GridView ID="AdhocJobGridView...

How to write data to a file in client side using javascript or someother client side scripting language.
Hi, I would like to know how to write a data to a client side file using javascript or someother client side scripting language. The file type expected is ini file. txt file would also be appreciable. Thanks in advance.   If the site is a trusted and you are running IE then you could try http://www.w3schools.com/asp/asp_ref_filesystem.asp The Old Sewing FactoryThat twitter nonsense. You can write to the server, but not to the client's machine.  Being able to do so would be a huge security breach.When you ask a question, remember to click "mark as answered"...

change Visible property of control using client side script
I have dataentry form in which there is a option for user to get all marketing material via email. if he click yes I have to enable table row containg that field or make visible property true of txtemail.text. I want this to do on clientside and if possible using vb.net. One option is javascript but I am using master pages and it will be kind of hard to put javascript. I am not sure let me know the best and smart way? Still not able to fix this. I am using masterpages in my website and I am not able to put javascript on anyother page expect site.master. How can I make panel control vis...

How to get necessary scripts, when using toolkit controls pure client side?
I'm using some controls from the toolkit by pure client side (quite powerful).For example: I want to use the popup extender and want to create it and attach it to something dinamically in javascript (without any server side, or aspx markup code).I have no problem to implement this.My question is how to ensure that all necessary script is sent down to the client. Currently I have found two possibilities:a) Add a dummy toolkit control to the page, that will ensure that we have the necessary scripts on the client side. This is ugly.b) Add manually the scripts in code behind, for example (wi...

Web resources about - Checking All CheckBoxes in a GridView Using Client-Side Script - asp.net.presentation-controls

Client-side - Wikipedia, the free encyclopedia
Client-side refers to operations that are performed by the client in a client–server relationship in a computer network . Typically, a client ...

Senior Search Manager - Client Side!!
... strategy insight for senior IT management - resources to understand and leverage information technology. Senior Search Manager - Client Side ...

Account Manager - Client Side - Previous Agency Experience Welcome!!
Computerworld Australia is the leading source of technology news, analysis and tools for IT decision makers, managers and professionals.

Media Sales Executive - Client Side!!
Technology news and reviews for business and home.

PPC Paid Search Manager - CLIENT SIDE
Defining Technology for Australia's IT Leaders. PPC Paid Search Manager - SEM - **CLIENT SIDE** Sydney CBD Location Client side!! SEM/PPC/Paid ...

How Basecamp Next got to be so damn fast without using much client-side UI
When we started working on Basecamp Next last year, we had much internal debate about whether we should evolve the existing code base or rewrite ...

Seven Tips for Pitching From the Client's Side of the Table
I had a chance to sit on the other side of the pitch table. Here is what I learned.

Client-side coding: How to prevent malicious use?
Applications that run on a user-controlled device cannot be perfectly protected.

The client side
... regarding the professionals you seek to assist. Even better if you possess that understanding yourself, having worked on the client side. I'm ...

Ecommerce traffic being hijacked by client side malware
According to new research 15 to 30 percent of eCommerce site visitors are infected with Client Side Injected Malware (CSIM) that causes them ...

Resources last updated: 1/22/2016 6:22:28 AM