p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Basics (http://p2p.wrox.com/forumdisplay.php?f=136)
-   -   DropDownList.SelectedValue weirdness (http://p2p.wrox.com/showthread.php?t=54986)

Aaron Edwards March 6th, 2007 01:51 PM

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


dparsons March 6th, 2007 01:58 PM

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
================================================== =========

Aaron Edwards March 6th, 2007 04:43 PM

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



dparsons March 6th, 2007 04:47 PM

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
================================================== =========

Aaron Edwards March 6th, 2007 05:08 PM

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


Aaron Edwards March 7th, 2007 12:39 AM

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



All times are GMT -4. The time now is 01:18 PM.

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