asp.net中通过DropDownList的值去控制TextBox是否可编写的实现代码
更新时间:2021年9月22日 10:18 点击:1787
效果:
.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;
}
}
相关文章
C#使用RichTextBox实现替换文字及改变字体颜色功能示例
这篇文章主要介绍了C#使用RichTextBox实现替换文字及改变字体颜色功能,结合实例形式洗了C#中RichTextBox组件文字替换及改变字体颜色相关操作技巧,需要的朋友可以参考下...2020-06-25- 这篇文章主要为大家详细介绍了C# TextBox数据绑定的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
WindowsForm实现TextBox占位符Placeholder提示功能
这篇文章主要介绍了WindowsForm实现TextBox占位符Placeholder提示,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-07-13C#实现winform中RichTextBox在指定光标位置插入图片的方法
这篇文章主要介绍了C#实现winform中RichTextBox在指定光标位置插入图片的方法,涉及RichTextBox控件及剪切板的相关操作技巧,非常简单实用,需要的朋友可以参考下...2020-06-25- 我们知道,DropDownList下拉框是一个服务器控件,有时候,有些朋友为了方便绑定DropDownList下拉框的选项,但又想在DropDownList实现客户端的下拉事件,那该怎么实现呢?...2021-09-22
- 这篇文章主要介绍了c# textbox的滚动条总是指向最底端的简单解决方法,需要的朋友可以参考下...2020-06-25
- 这篇文章主要为大家详细介绍了WPF TextBox实现按字节长度限制输入功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 最近在使用C# TextBox多行文本框的时候,发现了其对字数限制的一点问题,所以总结下在使用C# TextBox多行文本框要注意的的字数限制问题,现在分享给大家,有需要的朋友们可以参考借鉴,下面来一起看看吧。...2020-06-25
- 这篇文章主要介绍了C#中TextBox实现输入提示功能的方法,涉及C#中TextBox的相关操作技巧,需要的朋友可以参考下...2020-06-25
- C# TextBox 扩展方法数据验证详细说明,需要的朋友可以参考一下...2020-06-25
DropDownList获取的SelectIndex一直为0的问题
由于初始化判断出错导致每次传到服务器的时候会初始化一次,这就导致每次获取DropDownList的SelectIndex的时候只能是0...2021-09-22- 这篇文章主要为大家详细介绍了C# RichTextBox制作文本编辑器的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
.NET中TextBox控件设置ReadOnly=true后台取不到值三种解决方法
当TextBox设置了ReadOnly=true后要是在前台为控件添加了值,后台是取不到的,值为空,多么郁闷的一个问题经过尝试,发现可以通过如下的方式解决这个问题.感兴趣的朋友可以了解下...2021-09-22DropDownList 下拉框选择改变促发事件和防全局刷新(推荐)
这篇文章主要介绍了DropDownList 下拉框选择改变促发事件和防全局刷新(推荐)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2021-09-22ASP.NET笔记之 ListView 与 DropDownList的使用
本篇文章小编为大家介绍,ASP.NET笔记之 ListView 与 DropDownList的使用。需要的朋友参考下...2021-09-22- Asp.net之TextBox只允许输入数字的方法总结,需要的朋友可以参考一下...2021-09-22
在ASP.NET 2.0中操作数据之三十三:基于DataList和Repeater使用DropDownList过滤的主/从报表
前面已经介绍过使用DropDownList过滤的主/从报表,不过当时是基于GridView,本文算是复习一下,基于DataList和Repeater再次实现一下相同的功能。...2021-09-22- TextBox 控件为用户提供了一种向 ASP.NET 网页中键入信息(包括文本、数字和日期)的方法。...2021-09-22
ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法
这三个控件都有一个Items集合,可以用 RepeatLayout 和 RepeatDirection 属性来控制列表的呈现形式...2021-09-22ASP.NET DropDownListCheckBox使用示例(解决回发问题)
本文为大家介绍下ASP.NET DropDownListCheckBox的使用,这个是根据LigerUI改的,解决了回发问题,喜欢的朋友可以参考下...2021-09-22