【发布时间】:2010-08-26 19:36:50
【问题描述】:
我正在使用 ASP.NET 4.0,但在将所选值保存在 DropDownList 中时遇到问题。
我在 Web Control 的 PreRender 方法中初始化如下:
if (!Page.IsPostBack)
LoadCountryList();
LoadCountryList 的代码在哪里:
private void LoadCountryList()
{
var vr = new CountryRepository();
var countryList = vr.GetAllForList();
DdlCountry.EnableViewState = true;
DdlCountry.DataValueField = "code";
DdlCountry.DataTextField = "name";
DdlCountry.DataSource = countryList;
DdlCountry.DataBind();
}
提交表单后,我注意到 DropDownList 是空的(不是在回发之前)。如果你看一下我的代码,我已经为这个控件启用了 ViewState,但我还没有得到任何结果。有人知道怎么回事吗?
编辑:
下面是aspx文件中控件的简单代码:
<asp:DropDownList Runat="server" ID="DdlCountry">
</asp:DropDownList>
【问题讨论】:
-
DDL 是动态添加的,还是在 ASPX 中声明性的?
-
尝试将 enableviewstate="true" 放入标记而不是代码中。只是预感,我可能是错的......(我假设你在 @page 指令中有 enableviewstate="false",对吧?)
-
在哪里?我不知道去哪里找。
-
@Brian:三重检查是否没有代码重置下拉列表并且您不知道
-
没有,因为我已经创建了。
标签: asp.net