欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 14:09:18
效果: .aspx: 复制代码 代码如下: <%@ page language="c#" autoeventwireup="true" codefile="defau...
效果:
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;
}
}