dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersmonika
naveensanagasetti
Pawan Awasthi
Chandra
yadala
Pritom Nandy
pragna
Sidharthan
Lakshmi
HemzChand
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » ASP.NET GridView

How to use Gridview with checkbox control using asp.net


Posted Date:     Category: ASP.NET GridView    
Author: Member Level: Gold    Points: 10


In this article I have explained about How to use Gridview with checkbox control using asp.net. Each steps I have clearly explained in this article.



 


In this article I have explained about How to use Gridview with checkbox control using asp.net.
Each steps I have clearly explained in this article.You can check and uncheck checkboxes dynamically. You do not need to hard code the name of checkbox.
This code will automatically take care. While clicking Gridview header checkbox automatically column checkbox will checked or uncheked.





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>

<script type="text/javascript">



function SelectAllCheckboxesMoreSpecific(varspanChkbox)

{

var varIsChecked = varspanChkbox.checked;

var vChk = varspanChkbox;

Parent = document.getElementById('GridView1');

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

{

var vtr = Parent.rows[i];

var vtd = vtr.firstChild;

var vitem = vtd.firstChild;

if(vitem.id != vChk && vitem.type=="checkbox")

{

if(vitem.checked!= varIsChecked)

{

vitem.click();

}

}

}

}


</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderWidth="1px" CellPadding="4" Width="472px">

<Columns>

<asp:TemplateField HeaderText="Roles">

<HeaderTemplate>

<asp:CheckBox ID="chkSelectAll" onclick="SelectAllCheckboxesMoreSpecific(this);" runat="server" />

</HeaderTemplate>

<ItemTemplate>

<asp:CheckBox ID="chkSelect" runat="server" />

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField DataField="EmpID" HeaderText="Emp ID" ReadOnly="True" />

<asp:BoundField DataField="EmpName" HeaderText="Emp Name" ReadOnly="True" />



</Columns>



</asp:GridView>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataRow dtrow;
DataTable dtNewTable;
dtNewTable = new DataTable();
DataColumn dtcol, dtcol1;
dtcol = new DataColumn();
dtcol1 = new DataColumn();
dtcol.DataType = System.Type.GetType("System.String");
dtcol.ColumnName = "EmpID";
dtNewTable.Columns.Add(dtcol);
dtcol1.DataType = System.Type.GetType("System.String");
dtcol1.ColumnName = "EmpName";
dtNewTable.Columns.Add(dtcol1);
int i;
for (i = 0; i < 4; i++)
{


dtrow = dtNewTable.NewRow();
dtrow["EmpID"] = "";
dtrow["EmpName"] = "";
dtNewTable.Rows.Add(dtrow);

GridView1.DataSource = dtNewTable;
GridView1.DataBind();
}

}

}

}






Did you like this resource? Share it with your friends and show your love!


Responses to "How to use Gridview with checkbox control using asp.net"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to get gridview row id using asp.net
    Previous Resource: How to add checkbox to GridView rows in C#?
    Return to Resources
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.