Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

ASP.NET / DropDownlist JavaScript Problem


I have an ASP.NET DropDownList server control which gets populated on the client side via JavaScript, like so:

var oOption = document.createElement('OPTION');
oOption.value = sText;
oOption.text = sText;
listbox.options.add(oOption);

When a postback occurs, this ListBox doesn't contain crap. It's empty.

I've tried checking it for contents in Page_Load() and in some of the handlers. Nowhere does it seem to arrive with the items I added through JavaScript.
0
prashanthan
Asked:
prashanthan
1 Solution
 
razoCommented:
try calling the javascript function that populates the dropdownlist from the page_load event
0
 
RejojohnyCommented:
listbox, dropdownlist etc does not maintain the items added on the clients-side when a postback occurs as the viewstate does not know of these items added from the client (on the browser)
0
 
brdrokCommented:
hmmm....dunno if that is possible via javascript but maybe add the newly added items into the url string (cookies maybe) and then extract that new data inside the page_load or whevever?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ThaloxCommented:

yes Rejojohny is right.
But you can access the value the user selected in the dropdownlist.

on client add option with javascript
var oOption = document.createElement('OPTION');
oOption.value = "TestVal";
oOption.text = "TestVal";
listbox.options.add(oOption);

and on server you can use
Request.Form["listbox"]
this will return the selected value ("TestVal" in this case)

hope that helps
Thalox
0
 
prashanthanAuthor Commented:
I tried to retrive the value through Request.Form("listbox"), but it always return the first item of the dropdownlist.

Is there any other way to acheive it.

0
 
RejojohnyCommented:
Request.Form("listbox") will always give you the selected value in the listbox and not the whole list of items ..
0
 
RejojohnyCommented:
Please do have alook at my comments ..
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now