asp:DropDownList and Javascript

  • Thread starter Thread starter Britain Crooker
  • Start date Start date
B

Britain Crooker

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".
 
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:
(e-mail address removed).

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

--------------------
 
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?
 
Ok, what I described below MOSTLY seems to work, with one issue. If I
disable JavaScript in my browser, then (obviously) the SelectedIndexChanged
event will not fire until the user actually clicks the Submit button, rather
than firing immediately after a new selection is made.

Is there any way to determine which control initiated the PostBack?
Basically, I would want the SelectedIndexChange event to not fire (or exit
immediately) if the Submit button was pressed.

I have found some sample code for determining the PostBox control, but they
are all dependent on JavaScript.
 
Thanks for your reply Britain,

Sure, you can use the "SelectedIndexChanged" event of the first
dropdownlist to change the second(child) one's items. I didn't mention this
means in previous message because I assume that you may want a non-postback
approach(that will not refresh page).

As for the new question you mentioned, I'm afraid Javascript is necessary
here for you to provide additional information (whch control/elemente
trigger the submit postback....). A plain html form submit will not contain
any information about the source control who raise the postback/submit. Is
there any particular requirement on client-side that can not use javascript?

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:
(e-mail address removed).

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


--------------------
 
Hi Britain,

Have you got any progress on this issue? I'm still monitoring this thread
and welcome to post here if there is anything else we can help.\

Sincerely,

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:
(e-mail address removed).

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

--------------------
 

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

Back
Top