Page

How to get dropdownlist selected item value in asp.net

// Default.aspx //


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
  
  <asp:DropDownList ID="ddlMonth" Width="200px" runat="server">
            <asp:ListItem Value="0">--Select--</asp:ListItem>
            <asp:ListItem Value="1">January</asp:ListItem>
            <asp:ListItem Value="2">February</asp:ListItem>
            <asp:ListItem Value="3">March</asp:ListItem>
            <asp:ListItem Value="4">April</asp:ListItem>
            <asp:ListItem Value="5">May</asp:ListItem>
            <asp:ListItem Value="6">June</asp:ListItem>
            <asp:ListItem Value="7">July</asp:ListItem>
            <asp:ListItem Value="8">August</asp:ListItem>
            <asp:ListItem Value="9">September</asp:ListItem>
            <asp:ListItem Value="10">October</asp:ListItem>
            <asp:ListItem Value="11">November</asp:ListItem>
            <asp:ListItem Value="12">December</asp:ListItem>
        </asp:DropDownList><br /><br />
        <asp:Button ID="btnGetMonth" runat="server" Text="Get Month" 
            onclick="btnGetMonth_Click" /><br /><br />
        Selected Value :  <asp:Label ID="lblMonthVal" runat="server" Text="Label"></asp:Label>
<br />
        Selected Item :  <asp:Label ID="lblMonthItem" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>


// Default.aspx.cs //


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)

    {
     }
protected void btnGetMonth_Click(object sender, EventArgs e)
        {
            lblMonthVal.Text =  ddlMonth.SelectedValue; 
            lblMonthItem.Text = ddlMonth.SelectedItem.Text;

        }

}

No comments:

Post a Comment