0

我的表单上有两个 DropDownLists。两者都没有。在我的 Page_Load 事件中,我将它们分配给从私有方法返回的 DropDownList 对象。但是当页面加载时,两个下拉列表仍然是空的。我在返回填充下拉列表的方法内部进行了调试,它们包含项目。这样做有什么问题吗?

编码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddlUserTypes = UserTypesList();
    }
}

   private DropDownList UserTypesList()
{
    DropDownList ddList = new DropDownList();
    StaticDataProvider stDataProv = new StaticDataProvider();
    ddList.DataSource = stDataProv.UserTypes();
    ddList.DataTextField = "Value";
    ddList.DataValueField = "Key";
    ddList.DataBind();

    return ddList;
}

此外,当我 Response.Write() ddlUserTypes 的项目时,我会得到页面上列出的所有项目。物品似乎在那里,它们只是不可见。

4

5 回答 5

4

修复代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        UserTypesList(ddlUserTypes);
    }
}

   private void UserTypesList(DropDownList ddList)
{

    StaticDataProvider stDataProv = new StaticDataProvider();
    ddList.DataSource = stDataProv.UserTypes();
    ddList.DataTextField = "Value";
    ddList.DataValueField = "Key";
    ddList.DataBind();

}
于 2012-04-06T12:48:57.983 回答
0

Can we see your code? Web Forms or MVC ?

Have you tried setting the DataValueField and the DataTextField properties of the drop down list also are you calling the .DataBind() of the dropdown?

Really need to see your code in order to provide an answer..

于 2012-04-06T12:44:50.850 回答
0
if (!IsPostBack)
    {
        UserTypesList(ddlUserTypes);
    }


private void UserTypesList(DropDownList ddList)
{
    StaticDataProvider stDataProv = new StaticDataProvider();
    ddList.DataSource = stDataProv.UserTypes();
    ddList.DataTextField = "Value";
    ddList.DataValueField = "Key";
    ddList.DataBind();
}

您创建一个控件的新实例,然后简单地从页面中将其分配给当前实例。当您创建一个新控件时,您必须使用Controls.Add,这可能会在幕后使用其他东西。

于 2012-04-06T13:04:27.163 回答
0

这是一个猜测,没有看到任何代码。尝试在预渲染事件处理程序而不是负载处理程序中执行此操作。

于 2012-04-06T12:55:07.700 回答
-1

try

DropDownLists.Items.Refresh
于 2012-04-06T12:45:24.470 回答