asp.net dropdownlist without postback
The following asp.net c# example code demonstrate us how can we get the DropDownList web server control's
Selected item while DropDownList AutoPostBack property set to false. When we set the DropDownList AutoPostBack
property value to true then each time we change the item selection, DropDownList control causes a page post back to server.
When we working with a big form then AutoPostBack feature causes problem to accepting user data. So we need to disable the DropDownList AutoPostBack feature and we should submit web form manually to web server by button click. In this situation, when web form submitted to server then we need to collect user selected item from DropDownList.
In the c# script section we can get the DropDownList selected item programmatically by asking its SelectedItem property. DropDownList SelectedItem property returns the current selected item of DropDownList. So we can easily display the user selected item's text and value on web browser and can also perform other action.
When we working with a big form then AutoPostBack feature causes problem to accepting user data. So we need to disable the DropDownList AutoPostBack feature and we should submit web form manually to web server by button click. In this situation, when web form submitted to server then we need to collect user selected item from DropDownList.
In the c# script section we can get the DropDownList selected item programmatically by asking its SelectedItem property. DropDownList SelectedItem property returns the current selected item of DropDownList. So we can easily display the user selected item's text and value on web browser and can also perform other action.
dropdownlist-without-postback.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text = "you selected....<br />";
Label1.Text += "item: " + DropDownList1.SelectedItem.Text;
Label1.Text += "<br />value: " + DropDownList1.SelectedItem.Value;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net dropdownlist without postback</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - dropdownlist without 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="false"
>
<asp:ListItem Text="Greater Flamingo" Value="1"></asp:ListItem>
<asp:ListItem Text="Lesser Flamingo" Value="2"></asp:ListItem>
<asp:ListItem Text="Andean Flamingo" Value="3"></asp:ListItem>
<asp:ListItem Text="Chilean Flamingo" Value="4"></asp:ListItem>
<asp:ListItem Text="Wood Stork" Value="5"></asp:ListItem>
</asp:DropDownList>
<br /><br /><br /><br /><br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="show dropdownlist selected item"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</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 reset DropDownList on postback
- How to create a DropDownList displaying years
- How to sort ListBox items by value
- How to display different tooltip on ListBox items
- How to change ListBox item background color
- How to apply ListBox auto height
- How to count ListBox selected items
- How to remove selected items from ListBox