asp.net中通過(guò)DropDownList的值去控制TextBox是否可編寫的實(shí)現(xiàn)代碼
更新時(shí)間:2012年11月06日 11:10:53 作者:
Web窗體上有兩控件,DropDownList1,TextBox1,當(dāng)DropDownList的值選擇是YES的時(shí)候,TextBox1可編輯,當(dāng)選擇NO的時(shí)候,TextBox1的值為空,并且不能編輯,該如何實(shí)現(xiàn)
效果:

.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 List<string> GetData()
{
List<string> yn = new List<string>();
yn.Add("YES");
yn.Add("NO");
return yn;
}
}

.aspx:
復(fù)制代碼 代碼如下:
<%@ 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:
復(fù)制代碼 代碼如下:
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 List<string> GetData()
{
List<string> yn = new List<string>();
yn.Add("YES");
yn.Add("NO");
return yn;
}
}
您可能感興趣的文章:
- jquery獲取ASP.NET服務(wù)器端控件dropdownlist和radiobuttonlist生成客戶端HTML標(biāo)簽后的value和text值
- (asp.net c#)DropDownList綁定后顯示對(duì)應(yīng)的項(xiàng)的兩種方法
- asp.net DropDownList自定義控件,讓你的分類更清晰
- asp.net中不能在DropDownList中選擇多個(gè)項(xiàng) 原因分析及解決方法
- ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解
- ASP.NET筆記之 ListView 與 DropDownList的使用
- ASP.NET服務(wù)器端控件RadioButtonList,DropDownList,CheckBoxList的取值、賦值用法
- asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
- ASP.NET MVC中為DropDownListFor設(shè)置選中項(xiàng)的方法
- asp.net DropDownList實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果
- ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的4種方法
- ASP.NET 2.0中的數(shù)據(jù)操作之七:使用DropDownList過(guò)濾的主/從報(bào)表
- ASP.NET 2.0中的數(shù)據(jù)操作之八:使用兩個(gè)DropDownList過(guò)濾的主/從報(bào)表
相關(guān)文章
ASP.NET保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了ASP.NET保存PDF、Word和Excel文件到數(shù)據(jù)庫(kù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01asp.net 簡(jiǎn)單工廠模式和工廠方法模式之論述
簡(jiǎn)單工廠模式的最大優(yōu)點(diǎn)在于工廠類中包含了必要的邏輯判斷,根據(jù)客戶端的選擇條件動(dòng)態(tài)實(shí)例化相關(guān)的類,對(duì)于客戶端來(lái)說(shuō),去除了于具體產(chǎn)品的依賴2011-12-12讀取XML并綁定至RadioButtonList實(shí)現(xiàn)思路及演示動(dòng)畫
讀取XML的文檔,可以使用System.Data.DataSet類別中的ReadXml()方法,在aspx網(wǎng)頁(yè)上拉一個(gè)RadioButtonList控件,用來(lái)顯示XML的數(shù)據(jù),接下來(lái),用DataSet去讀取剛才寫好的獲取XML文件的屬性,即可完成2013-01-01ASP.NET My97DatePicker日期控件實(shí)現(xiàn)OA日期記事功能
這篇文章主要介紹了ASP.NET My97DatePicker日期控件實(shí)現(xiàn)OA日期記事功能的相關(guān)資料,需要的朋友可以參考下2016-04-04ASP.NET簡(jiǎn)化編輯界面解決思路及實(shí)現(xiàn)代碼(2)
這篇與前一篇改進(jìn)部分,也許大家會(huì)留意到動(dòng)畫演示,主要是GridVeiw的更新與刪除會(huì)在每row都有。因此Insus.NET把它抽取出來(lái),放在GridView外,感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01.Net Core中自定義認(rèn)證實(shí)現(xiàn)
本文主要介紹了.Net Core中自定義認(rèn)證實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01asp.net Google的translate工具翻譯 API
很久前的一天,我想使用這個(gè)東西,然后看了下,GooGle的Translate工具目前沒(méi)有公開(kāi)API,還是一個(gè)逐漸完善的過(guò)程,另一方面,利用一段很小的程序就可以得到我們想要的效果。2008-12-12