Loop through the grid view Records for selectAll check Box using JavaScript [Resolved]

Posted by Ksuresh under ASP.NET on 3/2/2012 | Points: 10 | Views : 4815 | Status : [Member] | Replies : 7
Hi every one,

Below cose of JavaScript is not woking for Select All check Boxes of Grid View Records.

var myform = document.forms[0];


i think the above Statement is not working, How to do this for Grid View control.


<script language="javascript" type="text/javascript">
function SelectAll(Id) {

var myform = document.forms[0];
var len = myform.elements.length;
document.getElementById(Id).checked == true ? document.getElementById(Id).checked = false : document.getElementById(Id).checked = true;
for (var i = 0; i < len; i++) {
if (myform.elements[i].type == 'checkbox') {
if (myform.elements[i].checked) {
myform.elements[i].checked = false;
}
else {
myform.elements[i].checked = true;
}
}
}
}
</script>


Thanks




Responses

Posted by: Hmanjarawala on: 3/3/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi check this attachment
 Download source file

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Ksuresh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/2/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi try this

<script language="javascript" type="text/javascript">

function SelectAll(Id) {



var myform = document.forms[0];

var len = myform.elements.length;

document.getElementById(Id).checked = document.getElementById(Id).checked == true ? false : true;

for (var i = 0; i < len; i++) {

if (myform.elements[i].type == 'checkbox') {

if (myform.elements[i].checked) {

myform.elements[i].checked = false;

}

else {

myform.elements[i].checked = true;

}

}

}

}

</script>


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Ksuresh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/2/2012 [Member] Bronze | Points: 25

Up
0
Down
Kindly visit this sites also.

http://www.devx.com/tips/Tip/20238
http://www.dotnetcurry.com/ShowArticle.aspx?ID=77

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Ksuresh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ksuresh on: 3/3/2012 [Member] Starter | Points: 25

Up
0
Down
This is my Actual Code

<%@ Page Language="C#" Theme="AdminDNF" MasterPageFile="~/AdminM/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="adminm_Blogs_Default" Title="Publish Family details to blog" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<asp:Label ID="lblTitle" runat="Server" SkinID="PageTitle">Post To Blog</asp:Label>
<asp:Label ID="lblMessage" runat="Server" SkinID="skMessage"></asp:Label>
<asp:Label ID="lblError" runat="Server" SkinID="skError"></asp:Label>
<table class="Search">
<tr>
<td>
Show records for <a href="Default.aspx?section=1">Articles</a> | <a href="Default.aspx?section=4">Questions</a> | <a href="Default.aspx?section=5">Interview Questions</a>
</td>
<asp:HiddenField ID="hidBlogEmailID" runat="Server" Value="dotnetfunda.blog@blogger.com" />
</tr>
</table>
<asp:GridView ID="GridView1" runat="Server" SkinID="RecordList" DataKeyNames="AutoID" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Select">
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server" onclick="SelectAll(this.id)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AutoID">
<ItemTemplate>
<asp:Literal ID="litAutoID" runat="Server" Text='<%# Eval("AutoID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "Category">
<ItemTemplate>
<asp:Literal ID="litCategory" runat="Server" Text='<%# Eval("Category") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "Author">
<ItemTemplate>
<asp:Literal ID="litAuthor" runat="Server" Text='<%# Eval("Author") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "Title">
<ItemTemplate>
<asp:Literal ID="litTitle" runat="Server" Text='<%# Eval("Title") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "Contents">
<ItemTemplate>
<asp:Literal ID="litBody" runat="Server" Text='<%# Eval("Body").ToString().Replace("\n", "<br />") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "Posted On">
<ItemTemplate>
<asp:Literal ID="litPosted" runat="Server" Text='<%# Eval("ThisDateTime") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Literal ID="litPaging" runat="server" EnableViewState="False" /> <hr />

<script language="javascript" type="text/javascript">
function SelectAll(Id) {

var myform = document.GridView1;
var len = myform.elements.length;
document.getElementById(Id).checked == true ? document.getElementById(Id).checked = false : document.getElementById(Id).checked = true;
for (var i = 0; i < len; i++) {
if (myform.elements[i].type == 'checkbox') {
if (myform.elements[i].checked) {
myform.elements[i].checked = false;
}
else {
myform.elements[i].checked = true;
}
}
}
}
</script>
</asp:Content>




Ksuresh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ksuresh on: 3/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Hmanjarawala ,

Thank you for your responses But I need using Java Script only.

what is the wrong in above JavaScript Code.

Thanks

Ksuresh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/3/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi suresh,
plz check it....i used your same code and done that using javascript.

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Ksuresh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ksuresh on: 3/3/2012 [Member] Starter | Points: 25

Up
0
Down
Sorry Hmanjarawala,

Actually I downloaded one of your source code from DotNetFunda.com i opened that one again from Downloads folder instead of this Download file.

Thank you, Hmanjarawala.

Thanks

Ksuresh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response