asp.net中通过DropDownList的值去控制TextBox是否可编写的实现代码
更新时间:2012年11月06日 11:10:53 作者:
Web窗体上有两控件,DropDownList1,TextBox1,当DropDownList的值选择是YES的时候,TextBox1可编辑,当选择NO的时候,TextBox1的值为空,并且不能编辑,该如何实现
效果:

.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数据绑定及使用详解
- 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过滤的主/从报表
相关文章
asp.net treeview checkbox 相关问题
asp.net treeview checkbox 相关问题,需要的朋友可以看下。2009-06-06
详解最好的.NET开源免费ZIP库DotNetZip(.NET组件介绍之三)
本篇文章主要介绍了.NET开源免费ZIP库DotNetZip组件的介绍,可以实现对文件的压缩和解压,有兴趣的朋友可以了解一下。2016-12-12
asp.net Javascript获取CheckBoxList的value
最近在做一个BS的小项目,记得自己搞asp.net的时候,还是两年以前,大部分的东西只是有点印象,忘得差不多了,所以这次也算是温习的过程吧,一边学习,一边赶工,呵呵呵。。。。2009-12-12


最新评论