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.
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.