Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript modified dropdown not having data on server

Status
Not open for further replies.

DarwinIT

Programmer
Apr 25, 2008
142
US
I am using an ASP:DropDownList with runat = "server"
In the javascript on that page - if another dropdown is modified the values of my dropdown is altered - with all items being removed and then applicable ones being added. When I submit the form - the item count is the original one I started with and the value is the text value for the one item I start with - the word ALL. How do I tweak the contentts of the dropdown so that the postback will get the right value??
 
don't use webforms :)
one, of many, problems with webforms is how it wants to make a stateless enviornment stateful.

A postback validates itself against the "previous" response ensuring that the form was not maliciously altered. part of this validation is checking an option selected by the user from a list/dropdown was part of the "previous" response. This information is stored in the viewstate. By changing these values on the client the viewstate still has the old values which are used to validate the postback. make sense?

if all you want is the new value of the selected item you can try:
1. keep your existing client code. when you extract the value on postback, pull the value from Request.Form["client name of control"), instead of MyDropDownList.SelectedValue. now this gets ugly because you need to build the client name (not id). so it would end up looking something like this
Code:
var clientId = MyDropDownList.ClientId;
var clientName = clientId.Replace("id separator", "name separator");
var value = Request.Form[clientName];
2. make the changes on the server instead of the client. This is poor option even with the use of MS AJAX UpdatePanel(s) because you need to submit a full postback (wrapped in ajax) for every change.
3. Try disabling, validate view state (or something like that). This is along the same lines as utilizing a Cascading Dropdown List with MS AJAX. there is a property on the page you need to set to false. This will bypass validating the postback is valid.
I'm not 100% sure about this, but it might be possible. Worth a shot anyway.

there is a 4th option... don't use webforms in favor of an MVC framework where you control the html.

however if you change the text of an option and you want to pull that value
Code:
var newText = MyDropDownList.SelectedItem.Text;
that's not possible. with or without webforms. The text of an option is like a label. it's read-only data that is not meant to be altered.

Instead you would need to do this:
1. keep the js you have to change the text of the Option.
2. after the value is changed, add an ajax call to update the data this Option represents on the server.
3. when the form is submitted get the value of row. query the data source (database, file, web service, in memory list, etc) for the new text value based on the id.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I peeked at the form values in the debugger. I found the values for the other 3 controls on my usercontrol. The one that is causing the problem had a value of Undefined. I actually ended up putting a textbox with a width of 0 on the page and assign the value to that when the dropdown is changed. Then I pull that info from the Form object and all is cool. Thanks so much for your feedback. This was a real dilemma!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top