Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check all Checkbox 1

Status
Not open for further replies.

hamking01

Programmer
May 30, 2004
238
US
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
 
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
 
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
 
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.
 
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
 
Actually, just found an article that does what i'm trying to do:
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?
 
Ecreations: Good looking site; down here in Florida the rotator is moving a tad bit too fast, fyi.
 
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.
 
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.
 
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.
 
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.
 
Thanx, works great. As you guys mentioned, this is causing a roundtrip to the server. Already got it working with jmeckley's link.
 
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.checked =chkVal&&!frm.elements.disabled?true:false;
}
else if (idVal.indexOf ('ChkBox') != -1)
{
if(frm.elements.name.indexOf('chkAll')>=0 && chkVal==false) frm.elements.checked=false;
}
}
}
</script>
 
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
 
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?
 
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.

[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
Got it, actually fixed it that afternoon. arunglobal's script will be fine since i only have checkboxes in the datagrid. thanx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top