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

(0)

相关推荐

  • (asp.net c#)DropDownList绑定后显示对应的项的两种方法

    方法一: 实现代码 复制代码 代码如下: DropDownList1.DataSource = listSort; DropDownList1.DataTextField = "LogSortName"; DropDownList1.DataValueField = "LogSortID"; DropDownList1.DataBind(); DropDownList1.SelectedIndex = ddl.Items.IndexOf(DropDownList1.

  • asp.net中不能在DropDownList中选择多个项 原因分析及解决方法

    示例代码: 复制代码 代码如下: BulkStockBll bll = new BulkStockBll(); DataSet ds = bll.GetBulkStock(); this.ddl_BulkStock.DataTextField = "Name" ; this.ddl_BulkStock.DataValueField = "ID" ; this.ddl_BulkStock.DataSource = ds; this.ddl_BulkStock.Data

  • ASP.NET 2.0中的数据操作之七:使用DropDownList过滤的主/从报表

    导言 主/从报表是一种很常见的报表, 这类报表中首先会显示一些主记录. 然后用户可以深入(追溯)某条主记录来查看该主记录的详情. 主/从报表是显示一对多关系的理想选择, 比如一个报表显示所有的产品类别,然后根据用户选择的特定类别显示与之关联的产品. 另外, 主/从表在显示"宽"表(有很多列的表)的详细信息时也很有用.例如主/从报表的主表部分显示数据库中产品表的产品名称和单价, 具体到某一个产品时再显示其他的产品字段(类别,供应商,单位数量,等等). 有很多方法可以实现主/从报表. 在这

  • asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法

    一.非强类型: Controller: ViewData["AreId"] = from a in rp.GetArea()                                select new SelectListItem {                                Text=a.AreaName,                                Value=a.AreaId.ToString()                   

  • ASP.NET笔记之 ListView 与 DropDownList的使用

    1.Repeater用来显示数据.ListView用来操作数据 InsertItemTemplate和updateItemTemplate**Eval(显示数据)和Bind(双向绑定:不仅是需要展现,更需要把数据绑定到数据库中) ItemPlaceholderID:占位符,决定占位,把头部(之上)和尾部(之下)分隔开ItemTemplate:展示功能 自动生成的ListView需要调整的地方(1.生成的样式要提到style中,不要用内联的方式(2.ItemTemplate里面一半没必要用<asp

  • ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法

    这三个控件都有一个Items集合,可以用 RepeatLayout 和 RepeatDirection 属性来控制列表的呈现形式.如果 RepeatLayout 的值为 Table,那么将在表中呈现列表.如果设置成 Flow,那么将在没有任何表结构的情况下呈现列表.默认情况下,RepeatDirection 的值为 Vertical.将此属性设置成 Horizontal 将会使列表水平呈现. RadioButtonList:控件提供已选中一个选项的单项选择列表(数据源单选).与其他列表控件相似,

  • asp.net DropDownList实现二级联动效果

    最近在做新闻发布系统的时候,用到了二级联动,我把使用方法记录下来,以便日后查阅以及帮助新手朋友们.下面是效果图: 下面来讲解一下实现的方法: 1.在.aspx页面中,拖入两个DroDownList控件.代码如下: <tr> <td>新闻风格:</td> <td><asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True&q

  • ASP.NET MVC DropDownList数据绑定及使用详解

    一:DropDownList 1.1 DropDownList绑定数据 1.1.1 DropDownList 固定绑定 这种方式适合那些已经固定的数据绑定到DropDownList上. 例 复制代码 代码如下: <asp:DropDownList runat="server" ID="ddlArea" Width="120px" > <asp:Listitem value="0">选择性别</as

  • ASP.NET 2.0中的数据操作之八:使用两个DropDownList过滤的主/从报表

    导言 在前面的指南中我们研究了如何显示一个简单的主/从报表, 该报表使用DropDownList和GridView控件, DropDownList填充类别,GridView显示选定类别的产品. 这类报表用于显示具有一对多关系的记录时非常合适, 同时它也可以很容易的被扩展以显示多个一对多关系的数据. 比如, 一个订单系统应该包含表示客户,订单和订单明细的表. 一个客户也许有多个订单,每个订单又包含多条订单项. 这样的数据可以使用两个DropDownList和一个GridView呈现给用户. 第一个

  • ASP.NET MVC中为DropDownListFor设置选中项的方法

    在MVC中,当涉及到强类型编辑页,如果有select元素,需要根据当前Model的某个属性值,让Select的某项选中.本篇只整理思路,不涉及完整代码. □ 思路 往前台视图传的类型是List<SelectListItem>,把SelectListItem选中项的Selected属性设置为true,再把该类型对象实例放到ViewBag,ViewData或Model中传递给前台视图. 通过遍历List<SelectListItem>类型对象实例 □ 控制器 public Action

  • jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值

    -.获取dropdownlist的text(ddlList为服务器端dropdownlist的ID,生成name属性等于ddlList的select标签) $("#ddlList option:selected").text() 二.获取dropdownlist的value(ddlList为服务器端dropdownlist的ID,生成name属性等于ddlList的select标签) $("#ddlList").val() 三.获取radiobuttonlist的t

  • ASP.NET中DropDownList下拉框列表控件绑定数据的4种方法

    DropDownList Web 服务器控件使用户能够从预定义的列表中选择一项.它与 ListBox Web 服务器控件的不同之处在于,其项列表在用户单击下拉按钮之前一直处于隐藏状态.另外,DropDownList 控件与 ListBox 控件的不同之处还在于它不支持多重选择模式. DropDownList在html中的呈现对应的是select,下面让我们来看一下DropDownList绑定数据的几种方法. 一.把Array数组绑到DropDownList 复制代码 代码如下: string[]

  • asp.net DropDownList自定义控件,让你的分类更清晰

    看到Discuz是2个下拉列表进行合并的,网上找了一些资料,然后写了这个小源码,在这里和大家分享一下! 运行效果图,如下所示(深黑的地方选不中,因为那是上一级的分类): 项目结构图如下所示: Controls类库SmartDropDownList.cs代码如下所示: SmartDropDownList.cs 复制代码 代码如下: using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using Syste

随机推荐