发布网友 发布时间:2022-04-27 14:55
共5个回答
懂视网 时间:2022-04-27 19:17
效果:
<%@ 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;
}
}
热心网友 时间:2022-04-27 16:25
给该DROPDOWNLIST控件添加OnSelectedIndexChanged事件
事件里添加JS代码,JS代码直接将选中的值赋给TEXTBOX就OK了
例如
OnSelectedIndexChanged="settextboxvalue(this.options[selectedIndex].value)"
然后再写这个JS函数
function settextboxvalue(val)
{
document.getElementById("textbox1").value = val;
}
思路是这样,但this.options[selectedIndex].value这句话有时候获取不到dropdowmlist的被选中的值热心网友 时间:2022-04-27 17:43
this.TextBox1.Text=this.DropDownList1.SelectedItem.Text;热心网友 时间:2022-04-27 19:17
将DropDownList的属性设为AutoPostBack="True"
触发SelectedIndexChanged事件写如下:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = this.DropDownList1.Text;
}热心网友 时间:2022-04-27 21:09
是在另一个 文本框里显示当前选中的内容吗?