Options added to <select> by javascript lost in postback

前端 未结 2 897
无人共我
无人共我 2020-12-11 01:17

I added some options to the select element by javascript in client side and cannot get it in postback.

What should I do?

Code used to add options:

         


        
2条回答
  •  隐瞒了意图╮
    2020-12-11 02:04

    The only data that is sent back to the server from the select tag is the value of the selected item. The options that you add are not sent back to the server.

    The server controls in ASP.NET uses viewstate to keep the ListItem objects that form the option tags in the rendered select tag. The viewstate is usually send in a separate hidden field so that it's returned to the server when the form is posted.

    You could use a similar technique for the options that you add, putting them also in a hidden field, which you then can process in the server code to create ListItem objects for the items to put in the DropDown control.

提交回复
热议问题