ACCESSING DROP DOWN LIST ITEM TEXT.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1ACCESSING DROP DOWN LIST ITEM TEXT.aspx.cs" Inherits="ACCESSING_DROP_DOWN_LIST_ITEM_TEXT" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
DesignTimeDropDownList:<asp:DropDownList ID="DropDownList1" runat="server" Height="16px" Width="179px">
<asp:ListItem Value="-1">Select City </asp:ListItem>
<asp:ListItem Value="1">Hyderabad</asp:ListItem>
<asp:ListItem Value="2">Karachi</asp:ListItem>
<asp:ListItem Value="3" Enabled="false">Lahor</asp:ListItem>
<asp:ListItem Value="4" Selected="True">Isalamabad</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
ACCESSING DROP DOWN LIST ITEM TEXT.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 ACCESSING_DROP_DOWN_LIST_ITEM_TEXT : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Jap Page Load Hoga to Phala se Set Hoga Field in the basic of index No
DropDownList1.SelectedIndex = 1;
//DropDownList1.SelectedValue="1";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if(DropDownList1.SelectedValue=="-1")
{
Response.Write("Please select a city");
}
else
{
//It is use to see selected item text
Response.Write("Selected Item Text is:"+DropDownList1.SelectedItem.Text+"<br>");
//It is use to see selected item value
Response.Write("Selected Item Value is:" + DropDownList1.SelectedItem.Value+"<br>");
//It is use to see selected item value
Response.Write("Selected Item Value is:" + DropDownList1.SelectedValue + "<br>");
//It is use to see selected item index
Response.Write("Selected Item Index is:"+DropDownList1.SelectedIndex+"<br>");
}
}
}
0 Comments