Html 'Select' control always returns the selected index as 0

I placed the HTML Select control on an aspx page and the elements (options) are loaded dynamically using javasvript. Items in the drop-down list are displayed on the web page. But when I select any item from the drop-down list, it selects the index is not returned in the aspx.cs file. In fact, it shows the selected index as 0 and the size of the “Select” html control as -1. I inserted javascript (which inserts elements in a drop-down list) in the body tag. I also tried calling the javascript function in Body onload. But it did not help. Please advice.

+3
source share
2 answers

Since you populated the list through javascript, the values ​​are not in the ViewState. Thus, when it returns, the code behind does not know the values ​​that are in the list.

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

+2
source

This is normal behavior. Why not bind the values ​​on the server side? Use <asp:DropDownList>instead <select>, give it an identifier and populate it from your .NET code before returning it to the client (maybe on the_Load page and make sure you check! IsPostBack before binding)

+1
source

Source: https://habr.com/ru/post/1756120/


All Articles