|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn one of my articles I discussed how to select checkboxes inside the GridView control. You can view the article here. This is an extension to that article in which, I will discuss some additional features. These features include using regular expression to limit the checkbox selection on a particular GridView and de-selecting the parent checkbox when all child checkboxes are un-checked. Creating the GridView ControlThe first task is to create a simple
Finally, here is the code for the <asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input type="checkbox" id="chkAll" name="chkAll"
onclick="Check(this)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCategoryName" runat="server"
Text = '<%# Eval("CategoryName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The Populating the GridView ControlI have used private void BindData()
{
string connectionString = "Server=localhost;
Database=Northwind;Trusted_Connection=true";
SqlConnection myConnection = new SqlConnection(
connectionString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT
CategoryID, CategoryName FROM Categories",
myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
Now, if you run the application you will see the
NOTE: I have added styles to make the Selecting and Deselecting CheckBoxesAt this point your If you take a look at the GridView HTML code you will notice that the parent checkbox ( function Check(parentChk)
{
var elements = document.getElementsByTagName("INPUT");
for(i=0; i<elements.length;i++)
{
if(parentChk.checked == true)
{
if( IsCheckBox(elements[i]) &&
IsMatch(elements[i].id))
{
elements[i].checked = true;
}
}
else
{
elements[i].checked = false;
}
}
}
The Check function is responsible for selecting and deselecting all the child checkboxes. First, I retrieve all the objects whose tag name is “INPUT”. Inside the loop I check that if the parent checkbox is checked or not. If it is checked then I find the correct checkbox element and mark it checked. The functions Before I explain the working of the two methods let’s see your little regular expression which separates the Here is out little regular expression: var pattern = '^GridView1';
The expression says that select every string whose name starts with “GridView1”. You might be thinking that how does this expression help to find the correct child checkboxes. In order to understand it let’s take a look at the HTML code that is generated when the
You can see that the checkboxes inside the Now, let’s take a look at the function IsMatch(id)
{
var regularExpresssion = new RegExp(pattern);
if(id.match(regularExpresssion)) return true;
else return false;
}
The function IsCheckBox(chk)
{
if(chk.type == 'checkbox') return true;
else return false;
}
All the code that we have discussed will allow you to check and uncheck all the checkboxes based on the parent checkbox. The code also checks for the particular Attaching Listeners to the Child CheckBoxesListeners allow you to catch events generated by the controls and take action on those events by firing a function. Scott Andrew wrote an excellent piece of code that attaches the listeners to the object. The code is browser compatible (I have tested on Mozilla and IE). function AddEvent(obj, evType, fn)
{
if (obj.addEventListener)
{
obj.addEventListener(evType, fn, true);
return true;
}
else if (obj.attachEvent)
{
var r = obj.attachEvent("on"+evType, fn);
return r;
}
else
{
return false;
}
}
I won’t be explaining the insights of this function but if you are really interested then you should check out this link. The function AttachListener()
{
var elements = document.getElementsByTagName("INPUT");
for(i=0; i < elements.length; i++)
{
if( IsCheckBox(elements[i]) &&
IsMatch(elements[i].id))
{
AddEvent(elements[i],'click',CheckChild);
}
}
}
In the code above I am registering the ‘click’ event of the checkboxes with a listener. Any time the click event is fired from the child checkboxes the <body onload="AttachListener()">
The CheckChild FunctionThe function CheckChild(e)
{
var evt = e || window.event;
var obj = evt.target || evt.srcElement
if(obj.checked)
{
if(counter < GetChildCheckBoxCount())
{ counter++; }
}
else
{
if(counter > 0) { counter--; }
}
if(counter == GetChildCheckBoxCount())
{ document.getElementById("chkAll").checked = true; }
else if(counter < GetChildCheckBoxCount())
{ document.getElementById("chkAll")
.checked = false;
}
}
The total of checked checkboxes is stored in the global variable called “counter”. Once, the counter reaches the total child checkboxes then the parent checkbox is checked. With the global variable counter in-place there is also a small change in the function Check(parentChk)
{
var elements = document.getElementsByTagName("INPUT");
for(i=0; i<elements.length;i++)
{
if(parentChk.checked == true)
{
if( IsCheckBox(elements[i]) &&
IsMatch(elements[i].id))
{
elements[i].checked = true;
}
}
else
{
elements[i].checked = false;
// reset the counter
counter = 0;
}
}
if(parentChk.checked == true)
{
counter = GetChildCheckBoxCount();
}
}
Few, extra lines are added to reset the counter. The method ConclusionI hope you liked the article, happy coding!
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||