×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

check all Checkbox

check all Checkbox

check all Checkbox

(OP)
I have a datagrid where first column are checkboxes.  I'm trying to add a button in column header, when click will check all checkboxes.  I've tried with the following for the onlclick of header button.  Could someone point to what i'm doing wrong, Thanx

Sub CheckAll (sender As Object, e as EventArgs)
    Dim dgi as DataGridItem
    Dim cb as Checkbox
    For each dgi In DataGrid1.Items
    cb = dgi.FindControl("selectRow")
        If cb.checked = False Then
            cb.Checked = True
        End If
    Next
End Sub

Following is the first column of the datagrid:

    <asp:TemplateColumn  
        Visible="True">
      <headertemplate>
          <input id="chkAllItems" type="button"
        onclick="CheckAll"/>
      </headertemplate>
      <itemtemplate>
        <asp:checkbox ID="selectRow" runat="server" />
      </itemtemplate>
    </asp:TemplateColumn>

When i click on the header button nothing happens, but IE displays a error page on status bar in bottom left.  Clicking on it, it states "Error: 'CheckAll' is undefined"

RE: check all Checkbox

How are you attaching the call to the CheckAll method to the button? The method you have shown above is VB.NET code and as such runs on the server. The error shown by IE is beacause you have made the call to CheckAll as if it was a clientside JavaScript or VBScript function. As there is no CheckAll function in client side code you get the error "CheckAll is undefined".

You can either make the button postback to the server and run the checkall method above on postback OR rewrite the method to use clientside scripting and run it clientside as you are attempting to at the moment.

The first option will probably be easier to code up especially as you have the CheckAll method written and I'm guessing you fairly unfamiliar with clientside scripting? However it does involve an unnecessary round trip to the server.

If you want some help to do this clientside you will need to post some source code for the datagrid (View > Source in IE) and indicate wether your target browser is IE only or if you require this tro work for other browsers too.

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953

RE: check all Checkbox

http://www.metabuilders.com/Tools/RowSelectorColum...
check out this website. There is a list of controls that are very handy. the link above is the control you are looking for.

all the coding is processed on the client, so you won't need to post back to the server.

The way it's coded only allows for 1 column like this. You'll have to tweak the code if you want to allow more than 1 per datagrid.

Jason Meckley
Database Analyst
WITF

RE: check all Checkbox

This code will Check all checkboxes in your Datagrid

CODE

Dim I as Integer
For i = 0 To dgi.Items.Count - 1
If Ctype(dgi.Items(I).FindControl("selectRow"),CheckBox.Checked=false
then
CType(dgi.Items(I).FindControl("selectRow"), CheckBox).Checked = True
End if
Next
i havent tested the code, let me know if it doesnt work for you.

The solution is simple, the problem is complex.
http://www.ecreationscanada.com

RE: check all Checkbox

(OP)
Ecreations: tried ur code but it doesn't work.  returns
BC30456: 'Items' is not a member of 'System.Web.UI.WebControls.DataGridItem'. for line:
For i = 0 To dgi.Items.Count - 1

RE: check all Checkbox

give me time to build a test app and i will let you know if i get to work

The solution is simple, the problem is complex.
http://www.ecreationscanada.com

RE: check all Checkbox

(OP)
Actually, just found an article that does what i'm trying to do: http://aspnet.4guysfromrolla.com/articles/122602-1...;

but since i'm still learning this stuff. it would be really helpful if you could explain your coding.  I looked into my previous codings and did something like this:

  Dim dgi as DataGridItem
  for each dgi In DataGrid1.Items

where dgi is an Items of Datagrid1.  For your code, do i need to refer the Items to Datagrid1 somewhere, if so, how?  

RE: check all Checkbox

Ecreations:  Good looking site; down here in Florida the rotator is moving a tad bit too fast, fyi.

RE: check all Checkbox

hamking
Finally got to test the code
aspx code
Notice the ToggleButton Runat="server"

CODE

<form id="Form1" method="post" runat="server">
            <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 264px; POSITION: absolute; TOP: 88px" runat="server" DataSource="<%# DataSet11 %>" autogeneratecolumns="False">
                <columns>
                    <asp:templatecolumn>
                        <headertemplate>
                            <asp:button id="ToggleButton" runat="server" onclick="CheckAll" text="Select all"/>
                        </headertemplate>
                        <itemtemplate>
                            <asp:checkbox id="selectRow" runat="server" />
                        </itemtemplate>
                    </asp:templatecolumn>
                    <asp:boundcolumn datafield="Forum_Description" sortexpression="Forum_Description" headertext="Forum_Description"></asp:boundcolumn>
                    <asp:boundcolumn datafield="Forum_Name" sortexpression="Forum_Name" headertext="Forum_Name"></asp:boundcolumn>
                    <asp:boundcolumn datafield="ID" sortexpression="ID" headertext="ID"></asp:boundcolumn>
                    <asp:boundcolumn datafield="Total_Questions" sortexpression="Total_Questions" headertext="Total_Questions"></asp:boundcolumn>
                </columns>
            </asp:datagrid>

This is the Public Function from Code Behind

CODE

Public Sub ToggleOnOFF(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim I As Integer
        For I = 0 To DataGrid1.Items.Count - 1
            If CType(DataGrid1.Items(I).FindControl("selectRow"), CheckBox).Checked = False Then

                CType(DataGrid1.Items(I).FindControl("selectRow"), CheckBox).Checked = True

            Else
                CType(DataGrid1.Items(I).FindControl("selectRow"), CheckBox).Checked = False
            End If
        Next
    End Sub

I have tested this code and it worked
I have one question though, why the extra trip to the server to check all checkboxes when you can use client javascript and save the server an extra request?

Good luck

The solution is simple, the problem is complex.
http://www.ecreationscanada.com

RE: check all Checkbox

Isadore,
Thanks I am working on coverting the site to asp.net
I have created another scroller as a user control (was a huge pain since i suck at Javascript) but its finally working.

It will take a couple of weeks or so to finish up and release the new site

The solution is simple, the problem is complex.
http://www.ecreationscanada.com

RE: check all Checkbox

change the name of Datagrid1 to your datagrid name dgi

in your case the sub code should look like this

CODE

Public Sub ToggleOnOFF(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim I As Integer
        For I = 0 To dgi.Items.Count - 1
            If CType(dgi.Items(I).FindControl("selectRow"), CheckBox).Checked = False Then

                CType(dgi.Items(I).FindControl("selectRow"), CheckBox).Checked = True

            Else
                CType(dgi.Items(I).FindControl("selectRow"), CheckBox).Checked = False
            End If
        Next
    End Sub
your button has to be a server control and sinc its contain in the datagrid template it doesnt have to be declared in code behind, thats taken care of by the Ctype statements

Hope that helps

The solution is simple, the problem is complex.
http://www.ecreationscanada.com

RE: check all Checkbox

Hamking,
managed to get your sub to work with a little bit of tweaking

try it

CODE

 Sub CheckAll(ByVal sender As Object, ByVal e As EventArgs)
        Dim dgi As DataGridItem
        Dim cb As CheckBox
        For Each dgi In DataGrid1.Items

            cb = CType(dgi.Cells(0).Controls(1), CheckBox)
            If cb.Checked = True Then
                cb.Checked = False
            Else
                cb.Checked = True
            End If
        Next
    End Sub
Hope that explains it better
Good luck

The solution is simple, the problem is complex.
http://www.ecreationscanada.com

RE: check all Checkbox

(OP)
Thanx, works great.  As you guys mentioned, this is causing a roundtrip to the server.  Already got it working with jmeckley's link.  

RE: check all Checkbox

Personally, the only way I would cause an extra trip to the server is if I know for sure that site browsers would disable Javascript

Good luck

The solution is simple, the problem is complex.
http://www.ecreationscanada.com

RE: check all Checkbox

Hi ,

Below code will make the SELECTALL/DESELECT ALL in Client side not in SERVER side.

Create a CheckBox  like this
----------------------------
<asp:CheckBox ID="chkAll" OnClick="javascript: return select_deselectAll (this.checked, this.id);"runat="server" AutoPostBack="false" ToolTip="Select/Deselect All"></asp:CheckBox>

Add this Java Script
====================

<script language="javascript">

function select_deselectAll (chkVal, idVal)
            {
                var frm = document.forms[0];
                // Loop through all elements
                for (i=0; i<frm.length; i++)
                {
                    // Look for our Header Template's Checkbox
                    if (idVal.indexOf ('chkAll') != -1)
                    {
                        // Check if main checkbox is checked, then select or deselect datagrid checkboxes
                        frm.elements[i].checked =chkVal&&!frm.elements[i].disabled?true:false;
                    }
                    else if (idVal.indexOf ('ChkBox') != -1)
                    {
                        if(frm.elements[i].name.indexOf('chkAll')>=0 && chkVal==false) frm.elements[i].checked=false;
                    }
                }
        }
</script>

RE: check all Checkbox

(OP)
I've used jmeckley's link and got it to work, however, it will only check all on my computer. When other users using the app on our intranet the check all function stops to work.  Any suggestions why?  

I went ahead and changed it using arunglobal's javascript and everythings working again.  Thanx

RE: check all Checkbox

(OP)
Actually, found that it was that particular user's computer that didn't work.  It worked for all other user's.  Still, does anyone have any ideas on why it doesn't work on this particular user's?  

RE: check all Checkbox

He may have javascript disabled (unlikely if he's successfully using ASP.NET), or he's using a different browser and the script isn't portable.

Of note is that the script arunglobal uses will check/uncheck EVERY checkbox on the page, not just those on your DataGrid.


Get a FREE iPod by helping me get mine! Click my referrer link:
http://www.freeiPods.com/default.aspx?referer=8158...

More about the company and deal:
http://www.wired.com/news/mac/0,2125,64614,00.html...

RE: check all Checkbox

(OP)
Got it, actually fixed it that afternoon.  arunglobal's script will be fine since i only have checkboxes in the datagrid.  thanx

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close