DropDownList

Скрыть

Показать

Копировать
  Default.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Value="#FF0000">Red</asp:ListItem>
    <asp:ListItem Value="#00FF00">Green</asp:ListItem>
    <asp:ListItem Value="#0000FF">Blue</asp:ListItem>
   </asp:DropDownList>
   &nbsp;
   &nbsp;
   &nbsp;
   <asp:Button ID="Button1" runat="server" Text="SelectedIndex" OnClick="Button1_Click" />
   &nbsp;
   &nbsp;
   &nbsp;
   <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
   &nbsp;
   &nbsp;
   &nbsp;
   <asp:Button ID="Button2" runat="server" Text="SelectedItem.Text" OnClick="Button2_Click" />
   &nbsp;
   &nbsp;
   &nbsp;
   <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
   &nbsp;
   &nbsp;
   &nbsp;
   <asp:Button ID="Button3" runat="server" Text="SelectedItem.Value" OnClick="Button3_Click" />
   &nbsp;
   &nbsp;
   &nbsp;
   <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
   &nbsp;
   &nbsp;
   &nbsp;
   <asp:Button ID="Button4" runat="server" Text="Clear" OnClick="Button4_Click" />
   <br />
   <br />
   <br />
   <br />
   <!-- ОБЯЗАТЕЛЬНО ДОБАВЬТЕ АТРИБУТ AutoPostBack="true" -->
   <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
    <asp:ListItem></asp:ListItem>
    <asp:ListItem Value="#FFFFFF">White</asp:ListItem>
    <asp:ListItem Value="#FFFF00">Yellow</asp:ListItem>
    <asp:ListItem Value="#800000">Brown</asp:ListItem>
    <asp:ListItem Value="clear">Clear</asp:ListItem>
   </asp:DropDownList>
   &nbsp;
   &nbsp;
   &nbsp;
   <asp:Label ID="Label4" runat="server" Text=""></asp:Label>
   &nbsp;
   &nbsp;
   &nbsp;
   <asp:Label ID="Label5" runat="server" Text=""></asp:Label>
   &nbsp;
   &nbsp;
   &nbsp;
   <asp:Label ID="Label6" runat="server" Text=""></asp:Label>
  </div>
 </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 Button1_Click(object sender, EventArgs e) {
  Label1.Text = DropDownList1.SelectedIndex.ToString();
 }
 protected void Button2_Click(object sender, EventArgs e) {
  Label2.Text = DropDownList1.SelectedItem.Text;
 }
 protected void Button3_Click(object sender, EventArgs e) {
  Label3.Text = DropDownList1.SelectedItem.Value;
 }
 protected void Button4_Click(object sender, EventArgs e) {
  Label1.Text = string.Empty;
  Label2.Text = string.Empty;
  Label3.Text = string.Empty;
  DropDownList1.ClearSelection();
 }
 protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) {
  Label4.Text = DropDownList2.SelectedIndex.ToString();
  Label5.Text = DropDownList2.SelectedItem.Text;
  Label6.Text = DropDownList2.SelectedItem.Value;
  if(DropDownList2.SelectedItem.Value == "clear") {
   Label4.Text = string.Empty;
   Label5.Text = string.Empty;
   Label6.Text = string.Empty;
   DropDownList2.ClearSelection();
  }
 }
}