Re: asp:DropDownList and Javascript

Tech-Archive recommends: Speed Up your PC by fixing your registry



Would it also be acceptable to update the first dropdown AutoPostBack=TRUE,
then put in the SelectedIndexChanged event the code to update the dropdown
items for the second control?

""Steven Cheng"" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:kMK0KXliIHA.360@xxxxxxxxxxxxxxxxxxxxxxxxx
Hi Britain,

From your description, you use javascritp to add some items into an
ASP.NET
dropdownlist, however, you found those added items disappear after the
page
be postback, correct?

I think this is the expected behavior due to ASP.NET webserver control's
control model. The control will construct its inner structure(and set
properites) at server-side, all those status will be persited in Viewstate
and render to client html. yes, you can modify the client-side html
elements, however, after you postback the page, the page will recreate the
control structure from viewstate, and your change at client-side will be
lost.

For your scenario, I think you can consider the following means:

1. You can consider add a hidden html field in page and add those items
(you add into dropdownlist via script) into that field, and after
postback,
always checks this field and syncrhonize those items in the field to your
dropdownlist's server-side item collection

2. You can consider use the ASP.NET ajax control (such as updatepanel) to
provide postback processing without refreshing the page:

#UpdatePanel Control Overview
http://asp.net/ajax/documentation/live/overview/UpdatePanelOverview.aspx

Best regards,

Steven Cheng
Microsoft MSDN Online Support Lead

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
From: "Britain Crooker" <britainc@xxxxxxxxxxxxxxxx>
Subject: asp:DropDownList and Javascript
Date: Wed, 19 Mar 2008 21:53:09 -0400


First - my MSDN Posting Alias isn't defined yet because the MSDN site is
having problems, it came back with " We were unable to communicate with
the
Managed Newsgroups registration system".

I have an ASP.NET website using C#. One page has two asp:DropDownList
controls, one that controls the other. Think a car website (first select
make, then model). I am using Javascript to populate the second list as
the
first one changes. The problem is that when I query these controls after
the Submit button is pressed, it always returns back the first value of
the
original list contents of the second control. It seems to be ignoring
what
the Javascript did during runtime. So if the original list had "1", "2"
and
"3" in the list, and then the user changes list 1 to another option, which
updates list 2 to have "4", "5" and "6" in the list. If the user clicks
submit and the code queries the value of list 2, it will return "1".






.


Quantcast