Question about client-side added options to dropdownlist

Y

yanni

I have some thing like this:

<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="uxTestDropDownList" runat="server" />
<asp:Button ID="uxSubmitButton" runat="server" Text="Submit"
OnClick="uxSubmitButton_Click" />
</div>
<script type="text/javascript">
var ddl = document.getElementById("uxTestDropDownList");
ddl.options[0] = new Option("Item1", "Item1");
ddl.options[1] = new Option("Item2", "Item2");
ddl.options[2] = new Option("Item3", "Item3");
</script>
</form>
</body>

And in uxSubmitButton_Click, it's just simply:
Response.Write(uxTestDropDownList.SelectedValue);

The "Item1" ~ "Item3" have been successfully added to DropDownList, but when
I click on uxSubmitButton, nothing print on page after post back... How to
make this work? (I have set EnableEventValidation="false" in page directive)

Thanks in advance!

Lei
 
C

Cowboy \(Gregory A. Beamer\)

If you are going to load client side, you will have to use the Request
object to find the control, by name, and pull its value.
 
B

bruce barker

all the browser posts back is the value. for the serverside control to
set a matching selected value, it must be in the list. if the server
knows the logic, it can add the dropdown value in OnInit, so the
selected value can be set (this woudl also allow validation to enabled).
your code could also look at the postback form collection, and add the
postback value in OnInint.

-- bruce (sqlwork.com)
 
B

bruce barker

viewstate is used to recreate the dropdown list on postback. its not
required if the list is built on postback in oninit. In fact its good
practice to disable viewstate to reduce page size.

-- bruce (sqlwork.com)
The "Item1" ~ "Item3" have been successfully added to DropDownList,
but when I click on uxSubmitButton, nothing print on page after post
back... How to make this work? (I have set
EnableEventValidation="false" in page directive)

The problem here is ViewState, which knows only about objects created
server-side.

So, you have two options:

1) Create the options server-side and use ViewState to reference them on
postback

2) Create the options client-side and use
Request.Form["uxTestDropDownList"] to reference them on postback
 
C

Cowboy \(Gregory A. Beamer\)

The OP has all of the values added client side, which negates OnInit(),
unless you are talking something other than the server side event handler.
Pulling the value from the form collection is his best option, as far as I
can see.


bruce barker said:
all the browser posts back is the value. for the serverside control to set
a matching selected value, it must be in the list. if the server knows the
logic, it can add the dropdown value in OnInit, so the selected value can
be set (this woudl also allow validation to enabled). your code could also
look at the postback form collection, and add the postback value in
OnInint.

-- bruce (sqlwork.com)
I have some thing like this:

<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="uxTestDropDownList" runat="server" />
<asp:Button ID="uxSubmitButton" runat="server" Text="Submit"
OnClick="uxSubmitButton_Click" />
</div>
<script type="text/javascript">
var ddl = document.getElementById("uxTestDropDownList");
ddl.options[0] = new Option("Item1", "Item1");
ddl.options[1] = new Option("Item2", "Item2");
ddl.options[2] = new Option("Item3", "Item3");
</script>
</form>
</body>

And in uxSubmitButton_Click, it's just simply:
Response.Write(uxTestDropDownList.SelectedValue);

The "Item1" ~ "Item3" have been successfully added to DropDownList, but
when I click on uxSubmitButton, nothing print on page after post back...
How to make this work? (I have set EnableEventValidation="false" in page
directive)

Thanks in advance!

Lei
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top