Re: asp:DropDownList and Javascript

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.

"Britain Crooker" <britainc@xxxxxxxxxxxxxxxx> wrote in message
news:uO3kScpiIHA.5208@xxxxxxxxxxxxxxxxxxxxxxx
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".








.



Relevant Pages

  • Re: Popup problems
    ... I am unable to find code examples to accomplish no postback in Javascript. ... >> and the results are presented to the visitor in a User control. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: onchange event in select element with keyboard scrolling in IE6
    ... Keypresses trigger keyboard events. ... `change' event is triggered at least in newer Geckos by change of selection ... the control does not need to lose focus first. ... Prototype.js was written by people who don't know javascript for people ...
    (comp.lang.javascript)
  • Re: Postback from javascript
    ... Yeah, as William suggest, if you simply want to postback using javascript, ... Or if you want to find out which control ... "Martin Eyles" wrote in message ... Is> there something else I can call from javascript that will submit the form -> maybe using the DOM? ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: asp:DropDownList and Javascript
    ... As for the new question you mentioned, I'm afraid Javascript is necessary ... Is there any way to determine which control initiated the PostBack? ... I would want the SelectedIndexChange event to not fire (or exit ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: LinkButton without Javascript???
    ... > Javascript disabled. ... In the application There is a control that looks ... > like a text hyperlink and it generates postback for server-side ...
    (microsoft.public.dotnet.framework.aspnet)