How to use DropDownList AutoPostBack in asp.net c#

DropDownList AutoPostBack Feature
DropDownList control allow users to select an item from a single selection drop-down-list. we can specify dropdownlist items by placing ListItem elements between the opening and closing tags of the dropdownlist control. .net developers can also populate dropdownlist from a data source object such as SqlDataSource, ObjectDataSource, LinqDataSource, ArrayList, Array etc. dropdownlist DataBind method bind the data source to the dropdownlist control.

users can select an item from dropdownlist and manually submit the form using submit button to send their selection into server. this way we can get user selection by clicking a submit button. but asp.net dropdownlist is a more user friendly server control. it has a property name AutoPostBack. AutoPostBack property works with SelectedIndexChanged event.

AutoPostBack property get or set a value that indicate whether a postback to the server automatically occurs when the user changes the list selection. OnSelectedIndexChanged method raises the SelectedIndexChanged event that allow developers to provide a custom handler for the event. SelectedIndexChanged event is raised when user change dropdownlist selection. so when if user change the dropdownlist item selection then page automatically post to the web server and developer can display user selection details on web page after postback occurs.

the following c# example source code describe you more how can you use dropdownlist AutoPostBack feature in asp.net to provide better user experience.
DropDownListAutoPostBack.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        Label1.Text = "You Selected: " + DropDownList1.SelectedItem.Text;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use DropDownList AutoPostBack feature</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">DropDownList: AutoPostBack</h2>
        <asp:Label 
             ID="Label1"
             runat="server"
             Font-Bold="true"
             ForeColor="Purple"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2"
             runat="server"
             Font-Bold="true"
             ForeColor="OrangeRed"
             Text="asp.net controls"
             >
        </asp:Label>
        <asp:DropDownList 
             ID="DropDownList1"
             runat="server"
             AutoPostBack="true"
             OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
             >
             <asp:ListItem>HyperLink</asp:ListItem>
             <asp:ListItem>PasswordRecovery</asp:ListItem>
             <asp:ListItem>PlaceHolder</asp:ListItem>
             <asp:ListItem>LoginName</asp:ListItem>
             <asp:ListItem>Label</asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>








More asp.net examples