473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Question about client-side added options to dropdownlist

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
Aug 1 '08 #1
7 1761
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.

--
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! |
********************************************
"yanni" <ya*******@126.comwrote in message
news:u2****************@TK2MSFTNGP06.phx.gbl...
>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
Aug 1 '08 #2
"yanni" <ya*******@126.comwrote in message
news:u2****************@TK2MSFTNGP06.phx.gbl...
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
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 1 '08 #3
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)

yanni wrote:
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

Aug 1 '08 #4
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)

Mark Rae [MVP] wrote:
"yanni" <ya*******@126.comwrote in message
news:u2****************@TK2MSFTNGP06.phx.gbl...
>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

Aug 1 '08 #5
"bruce barker" <no****@nospam.comwrote in message
news:eT****************@TK2MSFTNGP06.phx.gbl...
>>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

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.
How will this help since, in the OP's case, the list is built in client-side
JavaScript...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 1 '08 #6
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" <no****@nospam.comwrote in message
news:Ov****************@TK2MSFTNGP06.phx.gbl...
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)

yanni wrote:
>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
Aug 1 '08 #7
IC. Thanks guys.

Lei
Aug 2 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. I have marked the section from the book that is of intertest by tagging it with BOOK START HERE...
7
by: CT | last post by:
Hi, This might seem like a basic question but I have some doubts, please humour me. I have a client-server application using java where each client on each machine needs to directly...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
1
by: Brian Henry | last post by:
Hello, I was tring to learn socket's (being i never used them before) and have a simple question. I want to create a listner that will get any data recieved and print it out. I've been able to...
11
by: pshindle | last post by:
We have several machines currently running the DB2 V7 Run-time Client that we would like to actually be running the App Dev Client. To 'upgrade' (within the same version) this client software can...
2
by: et | last post by:
I am new to asp.net. I am writing a program that will revolve around an extensive client database, and wonder what the best way to design the program is, using classes. I have about 10 different...
1
by: Frank Millman | last post by:
Hi all I am developing a multi-user business/accounting application. It is coming along nicely :-), though rather slowly :-( I have hit an issue which will require a lot of changes to the code...
10
by: Robert | last post by:
I am an attorney in a non-profit organization and a self-taught programmer. I'm trying to create a client db that will allow me to search for potential conflicts of interest based either on Social...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
3
by: CeJay | last post by:
Hi All Ok this is a bit of a doozy for me. Work related question, I have been tasked with sorting out this issue at work, but don't really have much background in this field, but theres no one...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.