Effect:
. aspx:
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE html>
<title></title>
<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>
. Aspx.cs:
Copy Code code as follows:
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;
}
}