Click here to Skip to main content
13,151,125 members (45,011 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

2.5K views
1 bookmarked
Posted 11 Oct 2013

Validation & remeber values of selected checkboxes in grid controls using javascript

, 11 Oct 2013
Rate this:
Please Sign up or sign in to vote.
This article explains how to make validation on checkboxes and remember the selected checkboxes' values in DataGrid/GridView etc, together. This will

Editorial Note

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

This article explains how to make validation on checkboxes and remember the selected checkboxes' values in DataGrid/GridView etc, together. This will make xecution faster as we wont have to use FindControl method of grid controls.

I have always been in need of this and used this approach in almost every project.

//ASPX Code

<asp:GridView runat="server" AutoGenrateColumns="false">

<Columns>

<asp:TemplateField HeaderText="Select">

<ItemTemplate>

<input type="checkbox" id="chk_<%#Eval("id")%>" value="<%#Eval("id")%>" />

</ItemTemplate>

</asp:TemplateField>

...... (other column templates)

</Columns>

</asp:GridView>

Example: Suppose in control panel we are displaying registered users of the site in grid controls. And we want to provide a facility to activate/deactivate/delete these users by checking the appropriate checkboxes.

//JavaScript code

function getCheckboxSelection()
{
    var strID="";
    for (i=0; i < document.forms[0].elements.length; i++)
    {
        if ((document.forms[0].elements[i].type == 'checkbox') && (document.forms[0].elements[i].id.indexOf(ObjID) > -1))
        {
            if (document.forms[0].elements[i].checked == true)
            strID += document.forms[0].elements[i].value+",";
        }
    }
    if(strID!="") strID = strID.substring(0,strID.length-1);
    return strID;
}

If strID is "" then we can display error saying that "no checkbox was selected" else we can store strID (comma separated values of selected checkboxes) in HiddenField on aspx page which will be accessed in code behind to performed some operations on these selected values.

 

 

License

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

Share

About the Author

ASP.NET Community
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Collaborative Group

764 members


You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170924.2 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid