Monday, January 7, 2013

Get the dropdown selected value in asp.net using c#.net

Title:
How to get the drop down list selected value in asp.net using c#

Description:
The drop down list will display the list of items and each list of item is selectable.The best thing  of this control is ,we can bind static data or dynamic data which is to be done by using data binding concept.Now we know this control can support data binding

Example:
Some recent posts on drop down list selected value ,Jquery Drop down list validation .Now i would like to give an example on  how to get the drop down selected value in asp.net using c#.Before going to start ,we have to add one drop down list with label control which will be used to  display the drop down selected value .
Note:One more thing we need to remember i:e auto post back property set to true.If you want to see the properties of web control ,focus on control and click on F4

Aspx page:
<asp:DropDownList class="HTML" id="ddlcitytest" name="code" onselectedindexchanged="ddlcitytest_SelectedIndexChanged" runat="server"AutoPostBack="True">
<asp:ListItem>Please select</asp:ListItem>
<asp:ListItem>Hyderabad</asp:ListItem>
<asp:ListItem>Chennai</asp:ListItem>
<asp:ListItem>Bombay</asp:ListItem>
<asp:ListItem>Delhi</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblcity" runat="server" Text="Label"></asp:Label>

codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ddlcitytest_SelectedIndexChanged(object sender, EventArgs e)
{
//Get the selected value
lblcity.Text = ddlcitytest.SelectedValue.ToString();
}
}
In the above code the selected value has been assigned to label for display purpose.


Post a Comment