下拉列表重置SelectedValue

时间:2012-04-12 11:32:34

标签: asp.net drop-down-menu selectedvalue

我有一个连接到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;
}

4 个答案:

答案 0 :(得分:23)

您可以将DropSelection方法用于下拉列表

DropDownList.ClearSelection();

由于

迪普

答案 1 :(得分:2)

您应DataBind DropDownList if(!IsPostBack)并设置其默认值protected void Page_Load(Object sender, EventArgs e) { if(!IsPostBack) { int default = category.TypeId; DropDownList.SelectedValue = default.ToString(); } }

{{1}}

Page.IsPostBack Property

答案 2 :(得分:1)

将SelectedValue属性设置为空字符串:

DropDownList.SelectedValue = string.Empty;

答案 3 :(得分:0)

您可以将SelectedIndex设置为-1

DropDownList.SelectedIndex = -1;