C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

Gridview with checkbox header and item template


Posted Date: 25 Aug 2009    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: satyaMember Level: Diamond    
Rating: 1 out of 5Points: 12



Checkbox in Gridview:



The below javascript function is used to check and uncheck the chceck

boxes from gridview using header template checkbox of gridview.


<script type="text/javascript">
function CheckAndUnCheckCheckBoxes(GridviewId, CndChecked)
{
var checkIds = getCheckBoxes(document.getElementById(GridviewId));

for (i = 0; i <= checkIds.length - 1; i++)
{
checkIds[i].checked = CndChecked;
}
}

function getCheckBoxes(Id)
{
var checkboxesArray = new Array();
var inputGrd = Id.getElementsByTagName("input");

if (inputGrd.length == 0) null;

for (i = 0; i <= inputGrd.length -1; i++)
{
if(inputGrd[i].type == "checkbox")
{
checkboxesArray.push(inputGrd[i]);
}
}
return checkboxesArray;
}
</script>


Design View:



< asp:GridView Id="GridView1" runat="server" AutoGenerateColumns="false" >
< Columns >
< asp:BoundField DataField="EmployeeCode" HeaderText="Emp Code" / >
< asp:BoundField DataField="FirstName" HeaderText="First Name" / >
< asp:BoundField DataField="LastName" HeaderText="Last Name" / >
< asp:TemplateField >
< HeaderTemplate >
< input type="checkbox" onclick="CheckAndUnCheckCheckBoxes('< %= GridView1.ClientID % >',this.checked)" id="chk" / >
< /HeaderTemplate >

< ItemTemplate >
< asp:CheckBox ID="chkSelect" runat="server" / >
< /ItemTemplate >
< /asp:TemplateField >
< /Columns >
< /asp:GridView >

Code Behind:




//Connetion string from web.config

string sCon = ConfigurationSettings.AppSettings["Con"].ToString();

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}

private void BindGrid()
{
DataSet ds = SqlHelper.ExecuteDataset(sCon, "PS_WFM_UserDetails_Get");

if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
}


Attachments






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Gridview with checkbox header and item template  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to get the Values of Selected Row from a Gridview using ASP.NET?
Previous Resource: Alphabetic Gridview
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use