[RESOLVED]Traverse through gridview checkbox in script

hi, I have a grid and on a buttonclick i have to check wether atleast one checkbox in the gridview is checked.

in the first template fileld itself lies the checkbox…please suggest something as i need to alert and abort the postback if none of the checkboxes are checked.

THanks,

<asp:GridView ID="grdCompanyUser" CssClass="center" EmptyDataText="Users Not Available" runat="server" CellPadding="4" ForeColor="#333333"

GridLines="None" AutoGenerateColumns="False" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="ChkCompanyUser" runat="server" />
<asp:Label ID="lblCompanyUsrID" runat="server" Text=’<%# Eval("USRID") %>’ style="display:none;"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>

<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:LinkButton ID="lnkCompanyUsr" OnClick="lnkCompanyUsr_Click" runat="server">
<asp:Label ID="lblCompanyUsrName" runat="server" Text=’<%# Eval("Name") %>’>
</asp:Label>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="120px"></ItemStyle>
</asp:TemplateField>

<asp:TemplateField HeaderText="Email" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblCompanyUsrEmail" runat="server" Text=’<%# Eval("EmailID") %>’>
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="120px"></ItemStyle>
</asp:TemplateField>

<asp:TemplateField HeaderText="My Projects" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkCompanyUsrCount" OnClick="lnkCompanyUsrCount_Click" runat="server">
<asp:Label ID="lblCompanyUsrProjectCount" runat="server" Text=’<%# Eval("MyProjects") %>’></asp:Label>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="120px"></ItemStyle>
</asp:TemplateField>

<asp:TemplateField HeaderText="Status" HeaderStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:Label ID="lblCompanyStatus" runat="server" Text=’<%# (Eval("Status").ToString()=="True"?"Active":"InActive") %>’ ></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="120px"></ItemStyle>
</asp:TemplateField>

</Columns>

</asp:GridView>

Check if this works for you:

for (i=1; i<grid.rows.length; i++)
{
	cell = grid.rows[i].cells[0];

	for (j=0; j<cell.childNodes.length; j++)
	{
		if (cell.childNodes[j].type =="checkbox")
		{
			if(cell.childNodes[j].checked == 'checked')
			{
				//this is where you can decide if your checkbox is checked or not
				//and work accordingly
			}
		}
	}
}

function ConfirmSelected()
{
debugger;
var myGrid = document.getElementById("<%= GrdProjects.ClientID %>");
var flag = false;
var rows = myGrid.rows;

for (i = 0; i < rows.length; i++)
{

if (myGrid.rows(i).cells(0).children[0] != null && myGrid.rows(i).cells(0).children[0].checked == true)
{
flag = true;
}
if (flag) {
break;
}
}
if (flag)
{
return true;
}
}

i did it this way , it worked

function ConfirmSelected()
{
debugger;
var myGrid = document.getElementById("<%= GrdProjects.ClientID %>");
var flag = false;
var rows = myGrid.rows;

for (i = 0; i < rows.length; i++)
{

if (myGrid.rows[i].cells[0].children[0] != null && myGrid.rows[i].cells[0].children[0].checked == true)
{
flag = true;
}
if (flag) {
break;
}
}
if (flag)
{
return true;
}
}

i did it this way , it worked

Leave a Reply