Click here to Skip to main content
15,885,757 members
Articles / Grid
Article

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 5K   1  
This article explains how to make validation on checkboxes and remember the selected checkboxes' values in DataGrid/GridView etc, together. This will

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)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --