Link to home
Start Free TrialLog in
Avatar of ZekeLA
ZekeLAFlag for United States of America

asked on

DropDownList onchange causes other controls to lose values

I have  page with a datalist that contains two dropdownlists and several textbox controls. The two dropdownlists contain the same items but in different order. When the user selects an item in one dropdown, it also selects the same item in the other dropdown.

If I don't have the onchange event for the second dropdown, submitting the page will correctly validate the dropdownlists and textboxes which are all required fields. But with the second dropdown's onchange event, the dropdowns return to their initial unselected state and the textboxes are again empty.


The dropdowns have the following client side events:

ddlA.Attributes.("onchange") ="javascript: return changeDropDownA(this);"

Open in new window


ddlB.Attributes.("onchange") ="javascript: return synchDropDowns(this, '" & e.Item.FindControl("ddlA").ClientID & "');"

Open in new window


where the javascript functions are defined as follows:

  function changeDropDownA(e) {
    // other code to show / hide other visual elements on page goes here

    var eDropDownB = document.getElementById(e.id.replace(/ddlA/,'ddlB'));
    synchDropDown(e, eDropDownB);

    // more code to show / hide other visual elements on page goes here
  }

  function synchDropDown(src, ddl) {
    src = (typeof src == 'string') ? document.getElementById(src) : src;
    ddl = (typeof ddl == 'string') ? document.getElementById(ddl) : ddl;

    if (src && ddl) {
      ddl.value = src.value;
    }

    return true;
  }

Open in new window


If I change the second dropdown's onchange event to a simple "alert('changed');", the page posts back normally and the lists and textboxes keep their values. But if I add the synchDropDown event, they lose their values on postback.

I'm not sure how my code is preventing the datalist from preserving the control's values on postback.

Thanks in advance.
Avatar of Espavo
Espavo
Flag of South Africa image

Are your original values being set in a "If not IsPostback then... end if" on Page-Load?
That is something that I've found to cause this... (Not checking to see if the page is being posted-back when populating controls on-load)
Espavo
seems like somehow your Datalist is is databound upon postback.
As mentioned by Espavo you need to wrap your DataList databinding in if like:
if(!Page.IsPostBack){
//Databind datalist
}

OR share your code.
Avatar of ZekeLA

ASKER

No. The datalist and the controls have their viewstate enabled. The datalist is not rebound on postback. The datalist does have some controls which are dynamically loaded.

When I step through the page with and without the onchange function, the Datalist's ItemCreated event is hit in both cases. If attach the onchange event, the values are lost. If I don't, all is as expected.
Avatar of ZekeLA

ASKER

The problem with sharing the code is twofold. It's confidential so I'd have to get permission to share it. And if I can share it, I would have to share several thousand lines plus many other files in the project. I'll see if I can create a test page that reproduces the error but that may take a few days.
At what stage are your dynamically loaded controls being populated... are you 100% sure that it isn't the postback that's resetting the values?
Avatar of ZekeLA

ASKER

If I change the onchange assignment for ddlB from

ddlB.Attributes.("onchange") ="javascript: return synchDropDowns(this, '" & e.Item.FindControl("ddlA").ClientID & "');"

to

ddlB.Attributes.("onchange") ="javascript: alert('changed');"

the values remain. In both cases, I'm posting the page using a linkbutton outside of the DataList. In the latter case, the text values are what I entered. In the former case (with the onchange event), the values have been reset. Using Firefox's Error Console, there aren't any errors reported.
In the 1st case, what does synchDropDowns do? (Does it cause a PostBack?)
Avatar of ZekeLA

ASKER

It sets the target dropdown's value to the source's dropdown value.
Does it do this using JavaScript? (Client side)
Or does this happen on the server?
Avatar of MichaelMH
MichaelMH

There is no way to loose the values of other controls from your page unless you are performing a postback. Or if you a have function which when your event is triggered on the client side it does the cleaning. In case of poastback enable the viewstate (check this property value) of your controls and be sure you are not binding them with an empty datasource.
Avatar of ZekeLA

ASKER

I am posting back the page. But not from javascript. The postback comes from the linkbutton. The values are lost / maintained on postback depending on whether or not I have the javascript function in place. With this function assigned to it, the values are lost. With an alert('my message') assigned to it, the values are maintained.

I haven't yet tried a trace or reflector which are my next courses of action.
Avatar of ZekeLA

ASKER

I'm not able to compare behavior with and without Trace enabled. With the bad javascript function attached to the control, I get the following error:

System.Web.HttpException: Multiple controls with the same ID 'ctl00' were found. Trace requires that controls have unique IDs.

Does that suggest anything?
SOLUTION
Avatar of MichaelMH
MichaelMH

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'd put "If Not IsPostback Then" around all parts of your code where you are passing values to your controls, and see what happens...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial