Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 6th, 2011, 03:10 PM
Authorized User
Points: 160, Level: 3
Points: 160, Level: 3 Points: 160, Level: 3 Points: 160, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Accessing checkboxlist from code-behind

I have a form with a checkboxlist:

<asp:CheckBoxList ID="cblGames" runat="server" DataSourceID="sqlGames" DataTextField="Game" DataValueField="Id" </asp:CheckBoxList>
<asp:SqlDataSource ID="sqlGames" runat="server" ConnectionString="<%$ ConnectionStrings:Games %>"

<SelectCommand="SELECT [Id], [Game], [ShowWaitingList] FROM [Aanbod] WHERE ([ShowWaitingList] = @ShowWaitingList)">
<SelectParameters><asp:Parameter DefaultValue="true" Name="ShowWaitingList" Type="Boolean" /></SelectParameters>
</asp:SqlDataSource>

The code-behind is supposed to check the boxes according to values read in from a database. It fails to do so because it doesn't find the checkboxlist. This list has an ID of cblGames in both the page and the code-behind. So what am I doing wrong?

Regards, Guy

Code:
if    (!Page.IsPostBack && Request.QueryString["id"] != null)
        {
               // Code that gets the data goes here. Removed for brevity's sake //
              ListItem currentCheckBox = cblGames.Items.FindByValue(reader["AanbodId"].ToString());
              if (currentCheckBox != null)
              {
                  currentCheckBox.Selected = true;
              }
             
            }
  #2 (permalink)  
Old February 6th, 2011, 04:37 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Guy,

Quote:
It fails to do so because it doesn't find the checkboxlist.
Does it not find the list? Or the item you're trying to find? In the former case, are you getting some sort of compilation error?

In the latter case, try binding the list explicitly:

Code:
 
cblGames.DataBind();
if (!Page.IsPostBack && Request.QueryString["id"] != null)
Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #3 (permalink)  
Old February 6th, 2011, 05:55 PM
Authorized User
Points: 160, Level: 3
Points: 160, Level: 3 Points: 160, Level: 3 Points: 160, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It doesn't find the list. No error thrown.

If I replace the list generated in the aspnet tag with another (same name of course) but with hardcoded values, it does find it!

I am at a loss here, regards, Guy
  #4 (permalink)  
Old February 6th, 2011, 06:09 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

But how then do you know it doesn't find the list?

Have you tried calling DataBind? My guess is the list is there, but empty.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #5 (permalink)  
Old February 6th, 2011, 07:55 PM
Authorized User
Points: 160, Level: 3
Points: 160, Level: 3 Points: 160, Level: 3 Points: 160, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The checkboxlist is outputted and functional (it passes values to a database insert on submit).

The dynamically generated list box outputs the following source to the browser:

HTML Code:
<table id="MainContent_cblGames">
	<tr>
		<td><input id="MainContent_cblGames_0" type="checkbox" name="ctl00$MainContent$cblGames$0" value="1" /><label for="MainContent_cblGames_0">2/2 NL Texas</label></td>
	</tr><tr>
		<td><input id="MainContent_cblGames_1" type="checkbox" name="ctl00$MainContent$cblGames$1" value="2" /><label for="MainContent_cblGames_1">2/4 NL Texas</label></td>
	</tr><tr>
		<td><input id="MainContent_cblGames_2" type="checkbox" name="ctl00$MainContent$cblGames$2" value="3" /><label for="MainContent_cblGames_2">5/5 NL Texas</label></td>
	</tr>
</table>
The hard-coded (set up with the same values as returned by the dynamically generated) listbox outputs the following code to the browser:

HTML Code:
<table id="MainContent_cblGames">
	<tr>
		<td><input id="MainContent_cblGames_0" type="checkbox" name="ctl00$MainContent$cblGames$0" value="1" /><label for="MainContent_cblGames_0">Text1</label></td>
	</tr><tr>
		<td><input id="MainContent_cblGames_1" type="checkbox" name="ctl00$MainContent$cblGames$1" value="2" /><label for="MainContent_cblGames_1">Text2</label></td>
	</tr><tr>
		<td><input id="MainContent_cblGames_2" type="checkbox" name="ctl00$MainContent$cblGames$2" value="3" /><label for="MainContent_cblGames_2">Text2</label></td>
	</tr>
</table>
The code behind routine checks boxes only for the hardcoded listview and not the dynamically generated listview. They both output the same html before the populate routine runs.

Can you spot something my weary mind can't?

regards, Guy
  #6 (permalink)  
Old February 6th, 2011, 09:24 PM
Authorized User
Points: 160, Level: 3
Points: 160, Level: 3 Points: 160, Level: 3 Points: 160, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've edited this post which originally stated the problem had somehow miraculously solved itself by trying it on a fresh install of windows (clutching at straws!). It hadn't.
I erroneously retried the version with the hardcoded checkbox list, which had always worked.
I cannot get the dynamically generated checklist boxes to populate from the code-behind.
I have no idea why...

Help!

Last edited by guynorton; February 7th, 2011 at 10:31 AM..
  #7 (permalink)  
Old February 7th, 2011, 10:35 AM
Authorized User
Points: 160, Level: 3
Points: 160, Level: 3 Points: 160, Level: 3 Points: 160, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I see you are located in Utrecht. That's almost close enough to hear my screams of frustration! I live close to Breda.

Are there any asp.net courses in this region suitable for 40 year olds who work fulltime irregular hours....

Learning this in a vacuum is too painstaking and slow....
  #8 (permalink)  
Old February 7th, 2011, 12:05 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Ah, so that was you....

You can hire me through my company (http://devierkoeden.nl) for a personalized .NET training. Not cheap, but certainly worth it. I am also almost 40 and work irregular hours, so that shouln't be a problem ;-)

I don't know of any courses in your area but I know of some national training companies that can train you to become an ASP.NET TS (Technical Specialist) or MCP (Certified Professional). Not cheap either...

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #9 (permalink)  
Old February 7th, 2011, 01:34 PM
Authorized User
Points: 160, Level: 3
Points: 160, Level: 3 Points: 160, Level: 3 Points: 160, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I'm not sure what 'not cheap' entails but for reasons of discretion won't ask on this forum. I'll be contacting you via 'De Vier Koeden'....

With respect to the problem that I've been banging my head up against these last few days I'll keep the values hard-coded for the meantime and press on. Who knows, maybe a flash of inspiration will hit me, to be preceded by a d'oh!

regards, Guy

Last edited by guynorton; February 7th, 2011 at 01:45 PM..
  #10 (permalink)  
Old February 7th, 2011, 01:49 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

For the record: it's Koeden, not Koerden. E.g. animals, not people from Turkije, Syrië, Irak, Iran and the former Sovjet Union;-)

Quote:
Can you spot something my weary mind can't?
I still think that by the time your code executes, the DropDownList does not contain any values yet. Then "the list can be found", but it doesn't contain any items which in turn means you can't set a selected item. Calling DataBind before you try to preselect an item should do the trick as then the SqlDataSource executes its code and the list gets populated.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
accessing property from code behind in .ascx reks_dnet ASP.NET 1.0 and 1.1 Professional 1 May 23rd, 2007 08:35 AM
User.Identity.Name and accessing it from code CodeBugger C# 3 May 24th, 2006 04:41 PM
Accessing Drawing Objects from code isuru Excel VBA 0 August 29th, 2005 11:45 PM
Accessing Web User Controls from code aquaboltar ASP.NET 2.0 Basics 0 October 26th, 2004 04:04 PM



All times are GMT -4. The time now is 04:59 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.