Reset SelectedValue drop-down list

I have a DropDownList connected to an ObjectDataSource. In my page load, I set the selected value to a specific value depending on the case. How can I in my selectindexchange method "reset" the selected value so that you can still choose from other parameters? Or should I not use selectedvalue to set the default value in the drop-down list?

 <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;
}
+3
source share
3 answers

You should DataBind DropDownListand set the default value if(!IsPostBack):

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

Page.IsPostBack Property

+1
source

You can use the ClearSelection method for the drop-down list.

DropDownList.ClearSelection();

thank

Deep

+18
source

SelectedValue :

DropDownList.SelectedValue = string.Empty;
+1

All Articles