简体   繁体   English

Asp.net动态下拉列表

[英]Asp.net dynamic dropdownlist

I want to create a dynamic DropDownList in ASP.NET, but after postback the DropDownList loses the selected value. 我想在ASP.NET中创建一个动态的DropDownList ,但在回发后DropDownList会丢失所选的值。 Recreating the control on postback has no effect (ViewState, same ID). 在回发上重新创建控件无效(ViewState,相同的ID)。

Im wondering if the ViewState of the DropDownList is not working. 我想知道DropDownList的ViewState是否不起作用。 I've created dynamic TextBoxes before, and they retain the value after postback. 我之前创建了动态TextBox,它们在回发后保留了值。

I'm thinking to get the value from the form post like in this article: 我想从本文中的表单帖子中获取值:

http://www.aspsnippets.com/Articles/Creating-Dynamic-DropDownList-Controls-in-ASP.Net.aspx http://www.aspsnippets.com/Articles/Creating-Dynamic-DropDownList-Controls-in-ASP.Net.aspx

Is this a common solution to this problem? 这是解决这个问题的常见方法吗?

If you're not, you should be dynamically creating the drop-down on Init, and then you can read/write their properties on or after Load. 如果不是,则应该在Init上动态创建下拉列表,然后在Load上或之后读取/写入其属性。 Have a look at this article: http://www.4guysfromrolla.com/articles/092904-1.aspx which describes it in more detail. 看看这篇文章: http//www.4guysfromrolla.com/articles/092904-1.aspx ,它更详细地描述了它。

You have to recreate the control before ViewState is loaded, like during OnInit or OnPreInit for example. 您必须在加载ViewState之前重新创建控件,例如在OnInitOnPreInit期间。 You also need to assign the same ID to the control every time the page is posted back, because ViewState uses the control ID to reload the values. 每次回发页面时,您还需要为控件分配相同的ID,因为ViewState使用控件ID重新加载值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM