Wrox Programmer Forums

Need to download code?

View our list of code downloads.

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

You are currently viewing the ASP.NET 2.0 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
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 6th, 2007, 01:51 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default DropDownList.SelectedValue weirdness

For some reason my dropdown list's Selected Value property is being pre-populated with a value of "57", in quotes like that. I've tripple-checked and this is before I'm binding to anything. This is a problem because when I try to bind the dropdown against a data source it's throwing an error.

Anyone ever heard of this?

Aaron

Reply With Quote
  #2 (permalink)  
Old March 6th, 2007, 01:58 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm. No. Have you checked the physical tag of the Drop Down to see if maybe there is a tag out of place? Are you 100% certain that there is no binding or manually adding of items to the DDL?

What is the error the compiler is throwing?

You can use ddl.items.clear() to remove all items from the DDL collection, albeit that isn't a fix, more of a workaround to the actual problem.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
Reply With Quote
  #3 (permalink)  
Old March 6th, 2007, 04:43 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey thanks for the reply.

Here is the code for the ddl:
                                                    <asp:DropDownList Width="224px" ID="ddlFACID" runat="server">
</asp:DropDownList>

I searched the codebehind for anything that references the ddl and there isn't anything other than the code that I'm having problems with, which is...

Dim dvFac1 As DataView = dsLookupTables.Tables("Facilities").DefaultView
ddlFACID.DataSource = dvFac1
ddlFACID.DataValueField = "FacID"
ddlFACID.DataTextField = "FacilityName"
ddlFACID.DataBind()

The error that I get is "'ddlFACID' has a SelectedValue which is invalid because it does not exist in the list of items." When I debug I learn that the ddl for some reason already has a selectedValue of "57" (quotes included). Of course that is not in the dataView so it throws an error. The really weird thing is that I haven't even yet assigned a value to ddlFACID.SelectedValue. Ordinarily, I would do that right after binding to a data source.

Aaron


Reply With Quote
  #4 (permalink)  
Old March 6th, 2007, 04:47 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

What happens if you call

ddlFACID.Items.Clear()

right before you declare your Dataview?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
Reply With Quote
  #5 (permalink)  
Old March 6th, 2007, 05:08 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The same thing. I've tried...

        ddlFACID.SelectedValue = Nothing
        ddlFACID.Items.Clear()

as well (and tried them one at a time), but with the same results.

Aaron

Reply With Quote
  #6 (permalink)  
Old March 7th, 2007, 12:39 AM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well I figured this out.

It seems that the DropdownList's Selected Value property is only being set to "57" when the error is thrown. In other words, it starts out as "", which is what it should be, and then when I bind the ddl to a datasource, which certainly doesn't have "" as one of its items, it throws the error. That's all as it should be.

I caught it when, instead of debuggin and just waiting for the error to show up and then looking around, I stepped through the code from the beginning and watched the ddl's SelectedValue property closely. Granted, I should have done this earlier, but still, the property shouldn't have randomly changed itself when the exception was thrown like that.

Usually I bind my dropdowns declaritively, but this is a special case. I'm populating them manually from here on out, in a for each loop, then adding a null item at the top. Seems to be working.

Thanks.

Aaron

Reply With Quote
Reply


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
Array weirdness? oldmainframehack Beginning PHP 1 September 5th, 2007 03:24 PM
Need Help with DropDownList SelectedValue putputty ASP.NET 2.0 Basics 4 June 27th, 2007 11:44 AM
Unable to set SelectedValue on DropDownList jlrolin ASP.NET 2.0 Professional 0 April 13th, 2007 09:30 AM
Assigning a SelectedValue in a DropDownList rit01 ASP.NET 2.0 Professional 3 March 25th, 2006 01:12 AM



All times are GMT -4. The time now is 07:32 AM.


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