.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak
Subhransu Sekhar Jena
abhays
SP
DotnetUser

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Checkbox inside Gridview in asp.net

Posted By: Ramesh     Posted Date: November 12, 2008    Points:2   Category :ASP.Net
Hi,
I need to put checkbox inside gridview means for each row i need to have one check box just like yahoo or gmail, and one master check box on top of gridview to select/unselect all the row inside gridview.

I am using C# in code behind any help will be appreciated

Thanks in advance.

Responses
Author: Amit Mehra             
Posted Date: November 13, 2008     Points: 5   

Hi,
All you need to have Template column inside your gridview which should have checkbox and javascript function to seelect/unselect all the rows inside grid view

eg
<HeaderTemplate>
<input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" />
</HeaderTemplate>

For sample code please refer this article

http://www.gridviewguy.com/ArticleDetails.aspx?articleID=81

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: susant             
Posted Date: November 14, 2008     Points: 5   

Hi Ramesh check this to add each row one check Box.. and Amit code to add checkbox for Header section..
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ch1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Check this Link Also: http://www.dotnetspark.com/kb/125-gridview-with-checkbox-for-deleteing-checked.aspx


susant

Author: Suresh             
Posted Date: November 14, 2008     Points: 5   

Hi amit mehra
I got error like this,
(Error 1 The type or namespace name 'StringBuilder' could not be found (are you missing a using directive or an assembly reference?)

what to do.

Author: Amit Mehra             
Posted Date: November 14, 2008     Points: 5   

Hi Suresh,

you have to add System.Text namespace on your .cs file to access StringBuilder class for eg

using System.Text;

or give the fill name eg.

System.Text.StringBuilder

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Suresh             
Posted Date: November 14, 2008     Points: 5   

Hi Amit Mehra
Why did u give DatasourceID="mySource" in the gridview below code. can u plz explain clearly b'coz im new to asp.net.

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="PersonID" DataSourceID="mySource" Width="366px" CellPadding="4" ForeColor="#333333" GridLines="None">

Thank you for ur nice reply

Author: Suresh             
Posted Date: November 15, 2008     Points: 5   

Hi amit mehra

can u please explain this javascript.How this process is flow

<script type ="text/javascript" >

function SelectAllCheckboxes(spanChk){

// Added as ASPX uses SPAN for checkbox

var oItem = spanChk.children;

var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];

xState=theBox.checked;

elm=theBox.form.elements;

for(i=0;i<elm.length;i++)

if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)

{

//elm[i].click();

if(elm[i].checked!=xState)

elm[i].click();

//elm[i].checked=xState;

}

}

</script>

Author: Amit Mehra             
Posted Date: November 16, 2008     Points: 5   

HI,
When you attach the CheckBox to DataGrid This Javascript function will be fired to add this event to the checkbox you have to say like this

CheckBox.Attribute.Add("click","javascript:SelectAllCheckboxes(e);");

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: Suresh             
Posted Date: November 17, 2008     Points: 5   

Hi amith mehra
I knew that how javascript function firing. i want to how the process is flow inside the javascript.can u plz explain through command line in javascript. then only begginers can understand easily.

Author: Athira Sreejith             
Posted Date: December 31, 2008     Points: 5   

Hi..

Check with this design:

<asp:TemplateField HeaderText="Approved" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:CheckBox ID="chkCheckAll" runat="server" AutoPostBack="true" EnableViewState="true"
OnCheckedChanged="chkCheckAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="ChkStatus" AutoPostBack="true" runat="server" Checked='<%# Bind("IsStatus") %>'
OnCheckedChanged="ChkStatus_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>

Author: Clintonzz             
Posted Date: May 14, 2014     Points: 20   

Some materials towards gridview checkbox column might offer you some help:

http://www.kettic.com/winforms_ui/csharp_guide/gridview_column_types_checkbox_column.shtml



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend