Hi all,in previous we have discussed on data bind concepts for dropdownlist.So Then we have to know how get the item data when make a selection on drop down.Here in this example i have used one Asp button and dropdownlist. There are two methods to get the selected value.There is a direct method to get value which is "selected Value" and the other is Text of selected Index.Please comment if it is helpful
Code behind:
<html> <head runat="server"> <title>Get DropdownList selectedValue in asp.net</title> </head> <body> <form id="dynamic" runat="server"> <div> <asp:DropDownList ID="ddlType" runat="server"> </asp:DropDownList> <asp:Button id="btnGetSelectedvalue" runat="server" Text="Get selected Item value" onclick="btnGetSelectedvalue_Click" /> </div> </form> </body> </html>
Code behind:
public partial class GetDropdownSelectedvalueDB : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { //PopulateDropdownlist PopulateddlTypefromDataBase(); } } protected void btnGetSelectedvalue_Click(object sender, EventArgs e) { //selected value using Text method string DdlselectedText=ddlType.SelectedIndex.Text; //Get item using selected value string Ddlvalue=ddlType.SelectedValue.ToString(); Response.Write(DdlselectedText +"or"+Ddlvalue); } private void PopulateddlTypefromDataBase() { try { SqlConnection cnd = new SqlConnection(ConfigurationManager.ConnectionStrings["DdlConnectionString"].ToString()); cnd.Open(); SqlDataAdapter cad = new SqlDataAdapter("select *from Content", cnd); DataSet cds = new DataSet(); cad.Fill(cds, "Content"); ddlType.DataSource = cds; ddlType.DataTextField = "Content_Type"; ddlType.DataBind(); } catch (SqlException exd) { Response.Write(exd.Message.ToString()); } finally { cnd.Close(); } } }
No comments:
Post a Comment