DropDown SelectValue always first entry

S

Stephen Davies

I am sure you have all seen a post like this one but its not as easily solved
as the others as you will see from the description below:

Binding a DropDown to a database table (works well all present)
cbCountry.DataSource = objDs;
cbCountry.DataTextField = "CoName";
cbCountry.DataValueField = "CountryID";
cbCountry.DataBind();

Setting the default value to the clients setting from another table (works ok)
ListItem li = cbCountry.Items.FindByValue(SelectedValue);
if ( li != null )
cbCountry.SelectedIndex = cbCountry.Items.IndexOf(li);

User presented with form (correct value presented)
Either submit (with current value) or select a new value
Retrieve value and update database with

dbRowSc["CountryID"] = cbCountry.SelectedValue;

ALWAYS presented with FIRST value in the Dropdown list (yes I am checking is
postback in LoadPage event). Here is the traced output

PageLoad combo selected value is (before if IsPostBack) ->
In BIND of combo values currently set to NZ
USER CLICKS SUBMIT BUTTON
PageLoad combo selected value is (before if IsPostBack) ->AF
CustomValidator event (not related to this control) ->AF
ButtonSave event handler combo value ->AF
SaveValues combo Selected value ->AF

the trace event PageLoad is the first line in the event (AF is the first
entry in the combo)

Any Idea's I am pulling my hair out
 
S

Steve C. Orr [MVP, MCSD]

Either ViewState is disabled in one way or another, or a bug in your code is
resetting the list value upon every postback.
 
S

Stephen Davies

Some additional information that is interesting from the Trace output. I
added an extra line to see what the Request.Form variable was and there was
my value but not in the SelectedValue (value should be NZ as displayed in the
combo not AF which is the first value)

Here is the Trace output:

Trace Information
Category Message
aspx.page Begin Init
aspx.page End Init
aspx.page Begin LoadViewState
aspx.page End LoadViewState
aspx.page Begin ProcessPostData
aspx.page End ProcessPostData
PageLoad COMBO selected value is (before if IsPostBack) ->AF
From forms collection (Request.Form["HelperPanel1:cbCountry"]) ->NZ
aspx.page Begin ProcessPostData Second Try
aspx.page End ProcessPostData Second Try
aspx.page Begin Raise ChangedEvents
aspx.page End Raise ChangedEvents
aspx.page Begin Raise PostBackEvent
CustomValidator control (desperate measures) ->AF
ButtonSave event handler combo value ->AF
SaveValues combo Selected value ->AF
aspx.page End Raise PostBackEvent
aspx.page Begin PreRender
aspx.page End PreRender
aspx.page Begin SaveViewState
aspx.page End SaveViewState
aspx.page Begin Render
aspx.page End Render
 
S

Stephen Davies

Hi Steve
ViewState is enabled for that control

As you will see from the trace output (now the 3rd posting) the Form
variable is present but not the control value

Have performed a find through the code for the combobox control and double
checked, nothing is reseting it, only retrieving the SelectedValue property.

Regards
Stephen Davies

Steve C. Orr said:
Either ViewState is disabled in one way or another, or a bug in your code is
resetting the list value upon every postback.




Stephen Davies said:
I am sure you have all seen a post like this one but its not as easily
solved
as the others as you will see from the description below:

Binding a DropDown to a database table (works well all present)
cbCountry.DataSource = objDs;
cbCountry.DataTextField = "CoName";
cbCountry.DataValueField = "CountryID";
cbCountry.DataBind();

Setting the default value to the clients setting from another table (works
ok)
ListItem li = cbCountry.Items.FindByValue(SelectedValue);
if ( li != null )
cbCountry.SelectedIndex = cbCountry.Items.IndexOf(li);

User presented with form (correct value presented)
Either submit (with current value) or select a new value
Retrieve value and update database with

dbRowSc["CountryID"] = cbCountry.SelectedValue;

ALWAYS presented with FIRST value in the Dropdown list (yes I am checking
is
postback in LoadPage event). Here is the traced output

PageLoad combo selected value is (before if IsPostBack) ->
In BIND of combo values currently set to NZ
USER CLICKS SUBMIT BUTTON
PageLoad combo selected value is (before if IsPostBack) ->AF
CustomValidator event (not related to this control) ->AF
ButtonSave event handler combo value ->AF
SaveValues combo Selected value ->AF

the trace event PageLoad is the first line in the event (AF is the first
entry in the combo)

Any Idea's I am pulling my hair out
 
S

Steven Cheng[MSFT]

Hi Stephen,

As for the problem you mentioned, I'm also thinking it likely a viewstate
issue. Have you checked the page's viewstate setting. If both the page and
dropdownlist's Viewstate have been enabled, that really seems very strange.
Anyway, I think we can do some tests specific to the DropDownList. Would
you try create a new page which contains a DropDownList and use some test
datas to bind it and retrieve the selectedIndex/value in postback event?
(Make sure the page and dropdownlist is viewstate enabled).

Also, if you feel necessary ,I can send you a test page built on my side.
Please feel free to let me know if there is any other questions.

Thanks,


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Stephen Davies

Thanks for the reply Steven, looks like all the Steve's on this post.....

Created a new page in a new site, cut and pasted the appropriate chunks of
code from the original page and voila it works.

The original page must have some gunge in the working somewhere, never mind
I have the workaround by retrieving the Request.Form variable rather than the
combobox.SelectedValue. Maybe one day (when I get bored) I will rewrite the
page on a fresh slate.

Another unexplainable problem, but thanks for the assistance guys.

Regards
Stephen Davies
 
S

Steven Cheng[MSFT]

Thanks for your followup Stephen,

Yes, sometimes there does occur such puzzling problem. Anyway, glad that
you can continue your works.
Have a good day! :--)

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top