 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 6th, 2011, 03:10 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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;
}
}
|
|

February 6th, 2011, 04:37 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

February 6th, 2011, 05:55 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

February 6th, 2011, 06:09 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

February 6th, 2011, 07:55 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

February 6th, 2011, 09:24 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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..
|
|

February 7th, 2011, 10:35 AM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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....
|
|

February 7th, 2011, 12:05 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

February 7th, 2011, 01:34 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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..
|
|

February 7th, 2011, 01:49 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
 |