Monday, January 7, 2013

Get the dropdown selected value in using

How to get the drop down list selected value in using c#

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

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 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:Label ID="lblcity" runat="server" Text="Label"></asp:Label>

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.

No comments: