asp.net中通過DropDownList的值去控制TextBox是否可編寫的實現(xiàn)代碼
更新時間:2012年11月06日 11:10:53 作者:
Web窗體上有兩控件,DropDownList1,TextBox1,當DropDownList的值選擇是YES的時候,TextBox1可編輯,當選擇NO的時候,TextBox1的值為空,并且不能編輯,該如何實現(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:
復制代碼 代碼如下:
<%@ 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;
}
}
您可能感興趣的文章:
- jquery獲取ASP.NET服務器端控件dropdownlist和radiobuttonlist生成客戶端HTML標簽后的value和text值
- (asp.net c#)DropDownList綁定后顯示對應的項的兩種方法
- asp.net DropDownList自定義控件,讓你的分類更清晰
- asp.net中不能在DropDownList中選擇多個項 原因分析及解決方法
- ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解
- ASP.NET筆記之 ListView 與 DropDownList的使用
- ASP.NET服務器端控件RadioButtonList,DropDownList,CheckBoxList的取值、賦值用法
- asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
- ASP.NET MVC中為DropDownListFor設(shè)置選中項的方法
- asp.net DropDownList實現(xiàn)二級聯(lián)動效果
- ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的4種方法
- ASP.NET 2.0中的數(shù)據(jù)操作之七:使用DropDownList過濾的主/從報表
- ASP.NET 2.0中的數(shù)據(jù)操作之八:使用兩個DropDownList過濾的主/從報表
相關(guān)文章
ASP.NET保存PDF、Word和Excel文件到數(shù)據(jù)庫
這篇文章主要為大家詳細介紹了ASP.NET保存PDF、Word和Excel文件到數(shù)據(jù)庫的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01讀取XML并綁定至RadioButtonList實現(xiàn)思路及演示動畫
讀取XML的文檔,可以使用System.Data.DataSet類別中的ReadXml()方法,在aspx網(wǎng)頁上拉一個RadioButtonList控件,用來顯示XML的數(shù)據(jù),接下來,用DataSet去讀取剛才寫好的獲取XML文件的屬性,即可完成2013-01-01ASP.NET My97DatePicker日期控件實現(xiàn)OA日期記事功能
這篇文章主要介紹了ASP.NET My97DatePicker日期控件實現(xiàn)OA日期記事功能的相關(guān)資料,需要的朋友可以參考下2016-04-04ASP.NET簡化編輯界面解決思路及實現(xiàn)代碼(2)
這篇與前一篇改進部分,也許大家會留意到動畫演示,主要是GridVeiw的更新與刪除會在每row都有。因此Insus.NET把它抽取出來,放在GridView外,感興趣的朋友可以了解下啊,希望本文對你有所幫助2013-01-01asp.net Google的translate工具翻譯 API
很久前的一天,我想使用這個東西,然后看了下,GooGle的Translate工具目前沒有公開API,還是一個逐漸完善的過程,另一方面,利用一段很小的程序就可以得到我們想要的效果。2008-12-12