Drop down list selected value not submitting

N

nz mark in aus

Hi there

I am populating an empty <asp:dropdownlist on the client side using
Javascript using the following code

Heres my dropdown list
<asp:dropdownlist id="ddlDealers" Runat="server"></asp:dropdownlist>

heres the Java
var myDropDownList = frmMain.ddlDealers;

for (var cnt=0;cnt<=dealers.length-1;cnt = cnt + 2){
var opt = new Option(dealers[cnt],dealers[cnt],0,0);
myDropDownList.options[myDropDownList.options.length] = opt;
}
where dealers() is an array of values

the list is populated correctly and it all looks fine
my problem is that when I post the page using a <:asp Button the
selected value of the list is not being picked up on the server. Lets
say I selected 'Steve' in my drop down list, a simple test in my
Page_onload as follows returns NULL.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Write myDropDownList.SelectedValue
End Sub

The only time I can get the selected value from a dropdown is when I
have populated the drop down at the server in the normal .NET fashion,
but this requires an extra trip to the server which I am trying to
avoid.

Any ideas??

Mark
 
B

blackstaronline.net

Have you tried using

Request.Form("myDropDownList")

I have had issues with postbacks and grabbing .SelectedValue from a DD.
Request.Form always seems to help in those weird situations.

Hope it helps,
Jeremy Reid
http://blackstaronline.net
 
N

nz mark in aus

Hey that works thanks.

I guess because the Drop down list items aren't created on the server
side the client side selected item does not match with the server side
data, or something like that. Meaning you have to revert to a more
classic ASP style Request.Form

Thanks
Mark
 
D

Darren Kopp

It's because with dynamically added options, such as that, are not
persisted in ViewState, so the DDL control doesn't actually know
anything about those members.

When you post back, it parses the view state to look for the value you
are looking for. Request.Form gets the post variables, rather than
matching the posted selected value to the value in view state.
 
N

nz mark in aus

thanks for the info

So if I want to dynamically populate drop down lists, is using
Request.Form the best method to use or is there a more elegant
solution. I am using Callback to retrieve the list data from the
server

var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
etc..

can the viewstate be updated during this callback to the server?
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top