asp.net中通过DropDownList的值去控制TextBox是否可编写的实现代码

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了asp.net中通过DropDownList的值去控制TextBox是否可编写的实现代码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
效果:

.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;
}
}

脚本宝典总结

以上是脚本宝典为你收集整理的asp.net中通过DropDownList的值去控制TextBox是否可编写的实现代码全部内容,希望文章能够帮你解决asp.net中通过DropDownList的值去控制TextBox是否可编写的实现代码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。