asp.net中通过DropDownList的值去控制TextBox是否可编写的实现代码
程序员文章站
2024-03-05 14:09:18
效果: .aspx: 复制代码 代码如下: <%@ page language="c#" autoeventwireup="true" codefile="defau...
效果:
.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;
}
}
上一篇: Java的split方法使用详解