DropDownList Default Value after Databind

mattdaddym

New member
Joined
Apr 30, 2010
Messages
2
Programming Experience
3-5
Hi,

I have a dropdownlist that is databound to a sqldatasource. I need to set the default value of the dropdownlist to the username stored in a session variable.

I tried this in the Page_Load, but I get an error that the value does not exist. I assume this is because I am trying to set the value before the databinding has occurred.

I also tried to handle the OnBound event of the dropdownlist. This time I do not get an error, but the dropdownlist does not set to the value I would like. I have verified that the value I want is being bound to the dropdownlist and that my session variable value does equal what I believe it should.

In a nutshell. 1) dropdownlist is databound to sqldatasource 2) How can I set the default value programatically to a username variable?

Sorry, I'm not posting any code as there really isn't any to post. Hopefully just a straightforward rookie question. I found many similar situations when I searched, but most involved adding items to the list that weren't databound...not my situation. Thanks all!
 
Small breakthrough. It does work....sort of. I do something like this on the DataBound event of the dropdownlist.

myDropDownList.SelectedValue = session("mySessionVariable")

The problem now is that the list I bound to the dropdownlist contains usernames that are not all in the same case. Some are uppercase and some are lowercase. The case need to be correct for the above example to work. The weird part is, if I do this:

myDropDownList.SeelectedValue = "WrongValue"

I don't get an error....instead nothing is set and the data binds as usual. This is why I couldn't catch the problem before. Why doesn't the Page throw an error when the value doesn't exist? Something to do with the DataBound event?
 
It's just not made that way. The compiler doesn't care if the value doesn't exist. If it doesn't exist, it just doesn't select anything.

Change your select like this:
SELECT UPPER(userName) FROM MyTable;

Change your VB like this:
myDropDownList.SelectedValue = Session("mySessionVariable").ToUpper()
 
Back
Top