Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Javascript in GridView

My Gridview contains a template column which contains a checkboxlist control. Each row can have multiple checkboxes depening upon the data.

e.g (ALL, Code1, Code2, Code3 are checkboxes)

Row1 ALL Code1 Code2

Row2 ALL Code1

Row3 ALL Code1 Code2 Code3

How can I make sure that for each row, if user selects all the codes then ALL checbox will be automatically be checked and if only one code checbox is checked then ALL should not be checked. Please Guide, How I can achive this

Thanks

Shafiq
[601 byte] By [shafiqm] at [2007-11-11 8:33:49]
# 1 Re: Javascript in GridView
I am also having the same Exact problem.
i have seen ur Post in this Site.
But answer is not thier for ur question.
can u send that code.
which u tried.
i am waiting for ur reply.
same problem.

thanks
Seenu
cbe_seenu at 2007-11-11 23:13:13 >
# 2 Re: Javascript in GridView
hI Please send that Code
cbe_seenu at 2007-11-11 23:14:14 >
# 3 Re: Javascript in GridView
Try this below or see more at:-

http://www.codeproject.com/aspnet/Multi-select_Dataagrid.asp?df=100&forumid=20837&exp=0&select=1620652#xx1620652xx

function SelectAllCheckBoxes(theBox, gvSelected)
{
var xState=theBox.checked;
var elm=theBox.form.elements;
var strElmID;

for(i=0;i<elm.length;i++)
{
strElmID = elm[i].id;
if(elm[i].type=="checkbox" && strElmID!=theBox.id && strElmID.indexOf(gvSelected)!=-1 )
{
if(elm[i].checked!=xState)
{
elm[i].click();
}
}
}
}

Notice I am now searching for a string gvSelected in the id of the checkbox. Since the .NET naming convention will name all of the checkboxes with concatenation and numbering, all we have to do is specify a different id for each checkbox column, and search for that as we cursor through all the elements on the page.

The following is the code for the first gridview and the beginning of the second:

<asp:GridView ID="gvDefHrs" runat="server" AutoGenerateColumns="false" DataKeyNames="emp_def_earn_uuid"
DataSourceID="SqlDataSource1" OnRowCommand="gvDefHrs_RowCommand" OnRowDataBound="gvDefHrs_RowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAllDefHrs" runat="server" onclick="SelectAllCheckBoxes(this,'chkSelectDefHrs');"
type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelectDefHrs" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" CommandName="EditDefHrs" Text="edit" />
<asp:BoundField DataField="earn_hrs" HeaderText="Hours" SortExpression="earn_hrs" />
<asp:BoundField DataField="emp_def_earn_uuid" HeaderText="UUID" SortExpression="emp_def_earn_uuid"
Visible="false" />
</Columns>
</asp:GridView>

<asp:GridView ID="gvDefDollars" runat="server" AutoGenerateColumns="false" DataKeyNames="emp_def_earn_uuid"
DataSourceID="SqlDataSource2" OnRowCommand="gvDefDollars_RowCommand" OnRowDataBound="gvDefDollars_RowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAllDefDollars" runat="server" onclick="SelectAllCheckBoxes(this,'chkSelectDefDollars');"
type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelectDefDollars" runat="server" />
naijacoder at 2007-11-11 23:15:12 >