下拉列表重置SelectedValue

s.e*_*s.e 4 asp.net selectedvalue drop-down-menu

我有一个连接到ObjectDataSource的DropDownList.在我的页面加载中,我根据具体情况将s​​electedvalue设置为特定值.如何在我的方法中选择索引以"重置"所选值,以便您仍然可以从其他选项中进行选择?或者我不应该使用selectedvalue来设置下拉列表的默认值?

 <asp:DropDownList ID="DropDownList" runat="server" DataSourceID="ObjectDataSource" DataTextField="Type" DataValueField="TypeId" 
    AutoPostBack="true" onselectedindexchanged="DropDownList_SelectedIndexChanged" >
            </asp:DropDownList>

protected void Page_Load(object sender, EventArgs e)
{
        int default = category.TypeId;
        DropDownList.SelectedIndex = default;

}

protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{

    int id = int.Parse(DropDownList.SelectedValue);
    Session["id"] = id;
}
Run Code Online (Sandbox Code Playgroud)

Dee*_*nan 25

您可以使用ClearSelection方法下拉列表

DropDownList.ClearSelection();
Run Code Online (Sandbox Code Playgroud)

谢谢

迪普


Tim*_*ter 2

您应该只设置它的默认DataBind值:DropDownListif(!IsPostBack)

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        int default = category.TypeId;
        DropDownList.SelectedValue = default.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

Page.IsPostBack财产