select all check boxes using javascript

Ankitsrist
Posted by Ankitsrist under JavaScript category on | Points: 40 | Views : 1178
In this code lets see how to select all check boxes by using javascript firstly the code for template gridview
<asp:GridView ID="GridView1" DataKeyNames="id, name" runat="server"
AutoGenerateColumns="false" CssClass="Gridview" HeaderStyle-BackColor="#61A6F8"
ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onrowcommand="GridView1_RowCommand" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Height="184px" Width="489px">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
&nbsp;<asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" Height="40px"
ImageUrl="~/images/updateimage.jpg" />
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" Height="20px"
ImageUrl="~/images/updateimage.jpg" ToolTip="cancel" />&nbsp;
</EditItemTemplate>
<ItemTemplate>
&nbsp;<asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" Height="20px"
ImageUrl="~/images\editimagebutton.png"
ToolTip="edit" />&nbsp;
<asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete" Height="30px"
ImageUrl="~/images/deletebuttonimage.jpg" ToolTip="delete"/>
</ItemTemplate>
<FooterTemplate>
&nbsp;<asp:ImageButton ID="imgbtnAdd" runat="server" CommandName="insert" Height="20px"
ImageUrl="~/images/addimagebutton.jpg" ToolTip="insert" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate><asp:CheckBox ID="chkbx" runat="server" Onclick="javascript:chkAllCheckbox(this);" />Select all</HeaderTemplate>
<ItemTemplate><asp:CheckBox ID="checkboxes" runat="server"/></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<EditItemTemplate>
<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("name") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblitemUsr" runat="server" Text='<%#Eval("name") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtn" runat="server"/>&nbsp;
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="description">
<EditItemTemplate>
<asp:TextBox ID="txtcity" runat="server" Text='<%#Eval("decription") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%#Eval("decription") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtd" runat="server"/>&nbsp;
</FooterTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#61A6F8" Font-Bold="True" ForeColor="White" />
</asp:GridView>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="Button1" runat="server" OnClick="delete_Click"
Text="delete" /><br />
&nbsp;


then put this javascript code
function chkAllCheckbox(obj) 
{
var gv = document.getElementById('<%=GridView1.ClientID %>');
for (var i = 0; i < gv.all.length; i++)
{
var node = gv.all[i];;
node.checked = obj.checked;
}
}

and if you want to delete all selected records then write this code in code behind section
protected void delete_Click(object sender, EventArgs e)
{
dbc.myconnection();
//ArrayList chk = new ArrayList();
foreach (GridViewRow grv in GridView1.Rows)
{
CheckBox checkboxes = (CheckBox)grv.FindControl("checkboxes");
if (checkboxes.Checked)
{
int id = Convert.ToInt32(GridView1.DataKeys[grv.RowIndex].Values[0]);
//string id = grv.Cells[1].Text;
//chk.Add(id);
SqlCommand cmd = new SqlCommand("delete from dmlwithingrid where id=" +id, dbc.sqlcon);
cmd.ExecuteNonQuery();
}

}
fillgrid();
}

hope you got it...thanx for reading

Comments or Responses

Login to post response