Question about client-side added options to dropdownlist

  • Thread starter Thread starter yanni
  • Start date Start date
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
 
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)
 
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
 
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.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
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
 
Back
Top