Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 926
  • Last Modified:

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.
0
ZekeLA
Asked:
ZekeLA
  • 7
  • 5
  • 2
  • +1
2 Solutions
 
EspavoCommented:
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
0
 
guru_samiCommented:
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.
0
 
ZekeLAAuthor Commented:
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.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
ZekeLAAuthor Commented:
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.
0
 
EspavoCommented:
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?
0
 
ZekeLAAuthor Commented:
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.
0
 
EspavoCommented:
In the 1st case, what does synchDropDowns do? (Does it cause a PostBack?)
0
 
ZekeLAAuthor Commented:
It sets the target dropdown's value to the source's dropdown value.
0
 
EspavoCommented:
Does it do this using JavaScript? (Client side)
Or does this happen on the server?
0
 
MichaelMHCommented:
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.
0
 
ZekeLAAuthor Commented:
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.
0
 
ZekeLAAuthor Commented:
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?
0
 
MichaelMHCommented:
Well, it does, but I can't describe it to you in details since this would require to see the entire code.
Somehow you are duplicating the controls using javascript, you either cloning the DOM nodes (html elements) which contain your controls or you are assigning same ID to multiple elements from you page.
0
 
EspavoCommented:
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...
0
 
ZekeLAAuthor Commented:
I redesigned the page to only use a single dropdown.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now