asp.net dropdownlist resets on postback
DropDownList is an asp.net web server control which allow us to select an item from a drop-down list at a time.
dropdownlist AutoPostBack feature and SelectedIndexChanged method allow us to submit user selection to server when
dropdownlist items selection changed. when the dropdownlist selected index changed and its post back then the dropdownlist
show the selected item in its visible area. dropdownlist can display a single item at a time in its visible area.
but we need to reset dropdownlist and display first item on its visible area after selected index changed and postback. to do this, we need to call the dropdownlist ClearSelection() method. we call the method in SelectedIndexChanged event handling section end position. so we also can display the user selection details on web page and reset the dropdownlist.
dropdownlist ClearSelection() method clears out the list selection and set the selected property of all items to false.
the following asp.net c# example code demonstrate us how can we reset the dropdownlist on postback in an asp.net application.
but we need to reset dropdownlist and display first item on its visible area after selected index changed and postback. to do this, we need to call the dropdownlist ClearSelection() method. we call the method in SelectedIndexChanged event handling section end position. so we also can display the user selection details on web page and reset the dropdownlist.
dropdownlist ClearSelection() method clears out the list selection and set the selected property of all items to false.
the following asp.net c# example code demonstrate us how can we reset the dropdownlist on postback in an asp.net application.
dropdownlist-resets-on-postback.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = "you selected....<br />";
Label1.Text += "item: " + DropDownList1.SelectedItem.Text;
Label1.Text += "<br />value: " + DropDownList1.SelectedItem.Value;
//this line reset dropdownlist after select.
DropDownList1.ClearSelection();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net dropdownlist resets on postback</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - dropdownlist resets on postback
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="select an item from dropdownlist."
Font-Size="X-Large"
Width="350"
>
</asp:Label>
<br /><br />
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
>
<asp:ListItem Text="Sooty Shearwater" Value="1"></asp:ListItem>
<asp:ListItem Text="Manx Shearwater" Value="2"></asp:ListItem>
<asp:ListItem Text="Australasian Grebe" Value="3"></asp:ListItem>
<asp:ListItem Text="Little Grebe" Value="4"></asp:ListItem>
<asp:ListItem Text="Great Crested Grebe" Value="5"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>


- How to add an item to DropDownList after databind
- How to add a blank item at the top of DropDownList
- How to get DropDownList selected item without postback
- How to sort DropDownList items
- How to sort DropDownList items alphabetically
- How to sort DropDownList items by text
- How to sort DropDownList items by value
- How to select DropDownList item by value
- How to select DropDownList item by item text
- How to create a DropDownList displaying years