views:

52

answers:

2

I have placed HTML 'Select' control on aspx page and it's items(options) are loaded dynamically using javasvript. The items in dropdown appear properly on web page. But when I select any item from dropdown, it's selected index is not returned in aspx.cs file. In fact, it shows selected index as 0 and size of 'Select' html control as -1. I have inserted the javascript(which inserts items in dropdown) in body tag. I also tried by calling javascript function on Body onload. But it didn't help. Please advice.

+2  A: 

Hello

Because you populated the list via javascript, the values aren't in ViewState. So, when it posts back, the code behind isn't aware of the values that are on the list.

You could use Request.Form["ddWhatever"] to get the value of the selected item, but you lost the server side capabilities when you populated it on the client.

Jay Allard
+1  A: 

That's a normal behavior. Why not bind the values from the server side? Use an <asp:DropDownList> instead of <select>, give it an ID, and populate it from your .NET code before returning it to the client (Possibly on Page_Load, and make sure you check for !IsPostBack before binding)

SiN