效果:
![](/d/20211017/1060cda33466b72b82aefeeb4e3e487d.gif)
.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">
title>/title>
/head>
body>
form id="form1" runat="server">
asp:DropDownList ID="DropDownListYesNo" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
/asp:DropDownList>
asp:TextBox ID="TextBox1" runat="server" Enabled="true">/asp:TextBox>
/form>
/body>
/html>
.aspx.cs:
復制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Data;
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)
{
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
this.DropDownListYesNo.DataSource = GetData().Select(yn => new { value = yn }).ToList();
this.DropDownListYesNo.DataTextField = "value";
this.DropDownListYesNo.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (this.DropDownListYesNo.SelectedItem.Text)
{
case "YES":
this.TextBox1.Enabled = true;
break;
case "NO":
this.TextBox1.Enabled = false;
this.TextBox1.Text = string.Empty;
break;
}
}
private Liststring> GetData()
{
Liststring> yn = new Liststring>();
yn.Add("YES");
yn.Add("NO");
return yn;
}
}
您可能感興趣的文章:- jquery獲取ASP.NET服務器端控件dropdownlist和radiobuttonlist生成客戶端HTML標簽后的value和text值
- (asp.net c#)DropDownList綁定后顯示對應的項的兩種方法
- asp.net DropDownList自定義控件,讓你的分類更清晰
- asp.net中不能在DropDownList中選擇多個項 原因分析及解決方法
- ASP.NET MVC DropDownList數據綁定及使用詳解
- ASP.NET筆記之 ListView 與 DropDownList的使用
- ASP.NET服務器端控件RadioButtonList,DropDownList,CheckBoxList的取值、賦值用法
- asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
- ASP.NET MVC中為DropDownListFor設置選中項的方法
- asp.net DropDownList實現二級聯動效果
- ASP.NET中DropDownList下拉框列表控件綁定數據的4種方法
- ASP.NET 2.0中的數據操作之七:使用DropDownList過濾的主/從報表
- ASP.NET 2.0中的數據操作之八:使用兩個DropDownList過濾的主/從報表