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

