ASP.Net DropDownList选中的值

Sus*_*san 10 asp.net selectedvalue drop-down-menu

我有一种感觉,我错过了一些非常明显的东西,我无法捕获DropDownList的选定值; 该值将重新启动列表中的第一个项目.我已将DropListList autopostback属性设置为true.我有一个粘贴在下面的SelectedIndexChangedEvent.这不在母版页上.

protected void ddlRestCity_SelectedIndexChanged(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        r_city = ddlRestCity.SelectedValue.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是DropDownList控件:

<asp:DropDownList ID="ddlRestCity" runat="server" 
        Width="100px" AutoPostBack="True" 
        onselectedindexchanged="ddlRestCity_SelectedIndexChanged">
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)

Thanx提前为您提供帮助!

Fel*_*lan 12

我的袖口猜测是你可能会重新填充帖子后面的列表,这会导致所选索引重置.


Jus*_*yes 8

你的DataBind()电话在哪里?你!IsPostBack在通话前检查了吗?例如:

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
        ddlRestCity.DataSource = ...;
        ddlRestCity.DataBind();
    }
}
Run Code Online (Sandbox Code Playgroud)

说明:如果!IsPostBack之前没有检查过DataBind(),则列表将 触发之前重新填充SelectedIndexChanged(因为子事件之前Page.Load触发).当再被激发,在"选择项"现在是在新填充列表中的第一项.SelectedIndexChangedSelectedIndexChanged