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利用存儲(chǔ)過(guò)程實(shí)現(xiàn)模糊查詢示例分享
這篇文章主要介紹了asp.net利用存儲(chǔ)過(guò)程實(shí)現(xiàn)模糊查詢的示例,大家參考使用吧2014-01-01
asp.net treeview checkbox 相關(guān)問(wèn)題
asp.net treeview checkbox 相關(guān)問(wèn)題,需要的朋友可以看下。2009-06-06
ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)項(xiàng)目框架(二)
這篇文章主要介紹了ASP.NET?MVC5網(wǎng)站開(kāi)發(fā)項(xiàng)目框架,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09
詳解最好的.NET開(kāi)源免費(fèi)ZIP庫(kù)DotNetZip(.NET組件介紹之三)
本篇文章主要介紹了.NET開(kāi)源免費(fèi)ZIP庫(kù)DotNetZip組件的介紹,可以實(shí)現(xiàn)對(duì)文件的壓縮和解壓,有興趣的朋友可以了解一下。2016-12-12
ASP.NET MVC Webuploader實(shí)現(xiàn)上傳功能
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC Webuploader實(shí)現(xiàn)上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
asp.net Javascript獲取CheckBoxList的value
最近在做一個(gè)BS的小項(xiàng)目,記得自己搞asp.net的時(shí)候,還是兩年以前,大部分的東西只是有點(diǎn)印象,忘得差不多了,所以這次也算是溫習(xí)的過(guò)程吧,一邊學(xué)習(xí),一邊趕工,呵呵呵。。。。2009-12-12
ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)
本文主要介紹了ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

