Check all check boxes in Gridview

I have an itemtemplate in a gridview that has a check box control, and i have three pages on that gridview...Is there someone out there who knows how i can click a link button and no matter what happens all the checkbox for the datasource binded to the gridview are check??? basically a a check all functionality 


Give Yourself a round of applause!
DLP
Developer
0
Tryin2BGood
9/20/2007 1:20:09 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

9 Replies
1078 Views

Similar Articles

[PageSpeed] 5
Get it on Google Play
Get it on Apple App Store

I believe this article will guide you thru the process.

http://aspnet.4guysfromrolla.com/articles/052406-1.aspx

 


Peter Kellner
http://73rdstreet.com and blogging at
http://PeterKellner.net
MVP, ASP.NET
0
pkellner
9/20/2007 1:40:42 PM

Hi,

 You can find an example with a solution for you problem on this link:
http://www.gridviewguy.com/ArticleDetails.aspx?articleID=81

If you find this answer sufficient, please mark this question as solved!

 HaZ

0
haz
9/20/2007 1:52:56 PM

Thanks for the response but is there a way.....If i have a grid view that allows paging and i have 5 pages and i click check all and i go to page 2 i want page 2 to be checked.....is this possible...basically no matter how many pages i have in my gridiview when i click check all i want to know if i can get it to go across all pages????


Give Yourself a round of applause!
DLP
Developer
0
Tryin2BGood
9/24/2007 1:48:23 AM

Hi:

  You can use JavaScript to do this. Here's just a quick sample for DataList:

function Func(num)
{

for (i = 0; i <= Number(num); i++)
{
if(Number(i)<10)i="0" + i
id="DataList1_ctl" + i + "_CheckBox1"
document.getElementById(id).checked=true;

}

} 

 

<input id="Button1" runat="server"  type="button" value="button" />   <asp:DataList ID="DataList1" DataSourceID="SqlDataSource1" runat="server" OnPreRender="DataList1_PreRender">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox1" Checked="false" runat="server" />
        </ItemTemplate>
        </asp:DataList>
   protected void DataList1_PreRender(object sender, EventArgs e)
    {
      
        this.Button1.Attributes.Add("onclick","Func('" + (this.DataList1.Items.Count-1) + "')");
    }

 

  If you decide to use JavaScript there're many ways to do so.

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
9/26/2007 2:34:58 AM

 Hi,

      I guess following function shud help u..

 

Protected Sub CheckAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckAll.Click
   'Enumerate each GridViewRow
   For Each gvr As GridViewRow In FileList.Rows
      'Programmatically access the CheckBox from the TemplateField
      Dim cb As CheckBox = CType(gvr.FindControl("RowLevelCheckBox"), CheckBox)

      'Check it!
      cb.Checked = True
   Next
End Sub

Protected Sub UncheckAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UncheckAll.Click
   'Enumerate each GridViewRow
   For Each gvr As GridViewRow In FileList.Rows
      'Programmatically access the CheckBox from the TemplateField
      Dim cb As CheckBox = CType(gvr.FindControl("RowLevelCheckBox"), CheckBox)

      'Uncheck it!
      cb.Checked = False
   Next
End Sub
 All the best,
 
Love to work,
Nimesh

"The best way to learn is by practicing"
Please mark the post as answer if it has helped you
0
Nimay11
9/26/2007 6:18:02 AM

above is in vb.net

code in C#.net

//for check all buttons

protected void btncheckall_Click(object sender, EventArgs e)

{

 

foreach (GridViewRow gvr in g1.Rows)

{

CheckBox cb;

cb = (CheckBox)gvr.FindControl("CheckBox1");

cb.Checked = true;

}

 //for clear all buttons

protected void btnclearall_Click(object sender, EventArgs e)

{

foreach (GridViewRow gvr in g1.Rows)

{

CheckBox cb;

cb = (CheckBox)gvr.FindControl("CheckBox1");

cb.Checked = false;

 

}

}

0
harishaspdotnet
10/3/2008 10:20:05 AM

Tryin2BGood:

Thanks for the response but is there a way.....If i have a grid view that allows paging and i have 5 pages and i click check all and i go to page 2 i want page 2 to be checked.....is this possible...basically no matter how many pages i have in my gridiview when i click check all i want to know if i can get it to go across all pages????

check this post. u might be get an answer

http://forums.asp.net/t/1320257.aspx

 


Jai Ganesh. J , GSD ,India

Please Mark As Answer If my reply helped you.
0
Ganesh
10/3/2008 10:39:09 AM

 Hi,

Just add following code in ur linkbutton click event

 foreach (GridViewRow gvr in Gridview.Rows)
            {
                CheckBox chkSelect = (CheckBox)gvr.FindControl("Checkboxname");
                chkSelect.Checked = true;
            }


Please mark it as answer if this post seems to be useful.
0
nishit_123
10/3/2008 10:46:14 AM

Hi

try this example:

<script type="text/javascript">

    function SelectAll(id) {

        var frm = document.forms[0];

        for (i=0;i<frm.elements.length;i++) {

            if (frm.elements[i].type == "checkbox") {

                frm.elements[i].checked = document.getElementById(id).checked;

            }

        }

    } 

</script>

<!-- assuming that SqlDataSource1 is the datasource for my GridView -->

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Width="400px">

    <Columns>

        <asp:TemplateField>

            <AlternatingItemTemplate>

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

            </AlternatingItemTemplate>

            <ItemTemplate>

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

            </ItemTemplate>

            <HeaderTemplate>

                <asp:CheckBox ID="cbSelectAll" runat="server" Text="Select All" />

            </HeaderTemplate>

            <HeaderStyle HorizontalAlign="Left" />

            <ItemStyle HorizontalAlign="Left" />

        </asp:TemplateField>

    </Columns>

</asp:GridView>

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
 {

    if (e.Row.RowType == DataControlRowType.Header)
 {

        //adding an attribute for onclick event on the check box in the header

        //and passing the ClientID of the Select All checkbox

        ((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "')");

    }

}

and to get checked row for delete

foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox checkbox = (CheckBox)row.FindControl("cbxId");

            if (checkbox.Checked == true)
            {
                int Id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
               // your delete method

            }

        }
       

Good Luck

0
yasserzaid
10/4/2008 6:01:08 PM
Reply:

Similar Artilces:

Java script popup dialog box when a check box control is checked. How to do with a check box?
 I have a checkbox named cbxGetInfo. If someone checks the checkbox, I want to a popup dialog box confirming the action that will be done because of the checkbox being checked. A few months ago, I used a Java Script popup dialog box for a confirmation on an action being done. Now I need to figure out how to do this for when a check box has been checked. Can someone tell me what I am doing wrong. Here is the code I place in my C# code.    cbxGetInfo.Attributes.Add("onchecked", "javascript:if(confirm('Are you sure you want to delete this record?...

How to check how many check boxes are checked in the gridview
I am trying to make a javascript client fucntion to restrict no more then two rows could be selectedI have following in the itemtemplate of the gridview [although it is telerik control but same technique should be applied] <radG:GridTemplateColumn UniqueName="chkCheck">                    <HeaderStyle  HorizontalAlign="Right" />                    <HeaderTempla...

How to Check Check Box In GridView
I have One DropDown Bind With Some Procedure Table.And One GridView Bind Same Procedure Table and Grid View Have Templte Column,In Template Column Have Check Box.Both The ddl and GridView Working Properly. The Problem is there I want If Any Procedur Select in Dropdown then This Procedure Is Also check ijn GridView.     Atul try this javascript on the select onchange of dropdownlist  DgItemCount=4;//total gridview row count var CheckCount = 0; for ( i=2;i<DgItemCount + 2 ;i++) { if(document.getElementById("gridview1_ctl" + i + "_youritemname&qu...

Check boxes in Gridview not registering when being checked
 Hello, I have a grid view with some data and a Checkbox column, when a button is clicked i need the site to search the Gridview and add the checked rows data to an arraylist. When i check a box and click the button it steps into the loop but doesnt register that the combobox has been checked, can anyone help me understand why please? I must be missing something.   Dim selectedList As New ArrayList   'Arraylist to store the checked data in        For Each Row As GridViewRow In DGVAvailableCandidates.Rows   &n...

update table based on checkbox column of gridview ( if check box in gridview is checked )
 My gridview has checkbox column. Now, there is submit button on the form  . On its click event I am writing        For Each gvr As GridViewRow In GridView1.Rows            Dim chk As System.Web.UI.WebControls.CheckBox = TryCast(gvr.FindControl("CheckBox1"), CheckBox)            If (chk.Checked = True) Then  Sql = "Update [employer] set [Approved]='yes'"end if end next  But when i click the button...

Check All Check Box While Check Header CheckBox
 hello...I have two grid on same page...both grid contains header checkbox and item checkbox i want that if use check header checkbox then all item check box checked,i have javascript for this functinality but problem is that when i click on header check box of first grid then checkbox of second grid also checked that i dnt want i want to perform this operation on one grid at one time.i have attache my script also...plz help me....thanks<script type="text/javascript">        function SelectAll(ID)      &nbs...

how to check if a check box created in runtime checked or not?
I have a webform that have mutiple collectios of check boxes created on run time ( database driven) what is the best way to loop between them and get which ones has been checked by a user and save the results back to the database below a portion of my code. i am adding the checkbox to cell table which added to a row, then to a table, finlally the table added to a place holderwhile (drProfileList.Read()) {HtmlTableRow tr = new HtmlTableRow(); HtmlTableCell tc = new HtmlTableCell();CheckBox chkbox = new CheckBox(); chkbox.ID = drProfileList["RoleID"].ToString();chkbox.T...

Keeping check boxes checked when sorting GridView
I've got two columns of check boxes in a GridView. The problem I'm having is that check boxes that are checked become unchecked when the GridView is sorted.  Is it even possible to keep the check boxes in each row checked when the GridView is sorted? I was thinking they would stay checked because I have Enable ViewState set to true.  Any help is appreciated. Thanks  You can do it, but you'll need to write some code.  If you're using a DataSource control, when a Sort command is sent, the GridView saves the new sort data (sort expression, direction) a...

Checking Check Boxes
I've got a page that edits user permissions that I'm porting from PHP. When the page loads, I need the site to load a checkbox list with the name of the various items and the IDs as the value. This is easy. However, I also need it to check off the selected items. This information is stored in a seperate database table. Using a JOIN I can write a query that will return the page id, the page name, and the permissions of the user fairly easily. However, I don't know what attribute to set in order for appropriate boxes to be checked when the page loads. I'm posting this in the PHP m...

Check Box changes other check boxes
Hi, I have a question concerning changing a check box without doing a post back on the page. I would like to do something like this: Public Sub checkAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles checkAll.CheckedChanged If checkAll.Checked = False Then    checkVisio.Checked = True    checkPPT.Checked = True    checkRawArt.Checked = True    checkFlatArt.Checked = True Else    checkVisio.Checked = False    checkPPT.Checked = False    checkRawArt.Checked = F...

How to check for the existing user in database and using custom validator how to check check box is checked or not?
Hi Friends, For Registration page i want to check if that user already exist in database or not? and i also have to check if user have check the checkbox for i agree then only he will be able to complete registration. here i am sending my design. and all validation i have done all validation except these two.<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PRIZE MLS</title><link href="css/style.c...

check box databound in gridview as checked-item template
I have checked box in the  gird view which databound to table.   I want to check the data stores in the database as 1. then check box is show as checked.  it gave me error as  A call to Bind was not well formatted.  .aspx 86  if I do not databound as text. then the text of the check box show as true or false. <asp:TemplateField HeaderText="Annual Edu"> <EditItemTemplate> <asp:checkbox ID="ckAnnualEduE" runat="server" Checked='<%# bind(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "Ann...

Checked=Checked
 I am dynamically writing out checkboxes on a page.  I have 2 link buttons on the page that sets a boolean variable to True or False. This boolen is set in Page_PreInit and is used to Select or Deselect all the Checkboxes as they are written out. A Select ALL, Deselect ALL type of thing. At least that is the plan. As the page is being written out I do this for each checkbox and then add it to a table cell control that it added to a table row and on. tc is the table cell.  Dim chkItem As CheckBox = New CheckBox chkItem.ID = "chkRecommend" + drMember("ID")...

Check in
Hi all. I am migrating from PB 7.0 to PB 10.5, After migrating my first application i am trying to work on my first window but i'am not able to find out the way to CHECK it OUT. Wher do i can find "check out/check in" functions ?? TIA Mario Parietti What source control are you using? Have you configured it? Right click on your Workspace and check the source control tab to see if you've got it set up. "Mario Parietti" <mparietti@intercad.ch> wrote in message news:451283e0$1@forums-1-dub... > Hi all. > I am migrating from PB...

Web resources about - Check all check boxes in Gridview - asp.net.presentation-controls

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 11/28/2015 3:08:13 PM