当DropDownList中的值变化时,<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
文本框txtname的值也跟着变化.
但是问题是,不能启用AutoPostBack,因为会刷新掉别的文本框的值.
想到用js实现可能是最好的方式,但是不知道怎么写,希望有高手指点,必留代码啊...!
文本框txtname的值也跟着变化.
但是问题是,不能启用AutoPostBack,因为会刷新掉别的文本框的值.
想到用js实现可能是最好的方式,但是不知道怎么写,希望有高手指点,必留代码啊...!
希望有高手指点,主要是不用AutoPostBack能实现的办法
function MySelect()
{
var ddl = document.getElementById("DropDownList1");
var avalue=ddl.options[ddl.selectedIndex].value ;
document.getElementById("TextBox1").value=avalue;
}c#,在page_Load中
DropDownList1.Attributes.Add("onchange", "MySelect()");
<head runat="server">
<title>DropDownList与TextBox关联</title>
<script type="text/javascript" language="javascript">
function ddlchange()
{
var ddl=document.getElementById("<%=ddl01.ClientID %>");
var tb=document.getElementById("<%=txtname.ClientID %>");
tb.value=ddl.value;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lbl01" runat="server" Text="请选择你所在城市" />
<asp:DropDownList ID="ddl01" runat="server" onchange="ddlchange()">
<asp:ListItem>北京</asp:ListItem>
<asp:ListItem>上海</asp:ListItem>
<asp:ListItem>成都</asp:ListItem>
<asp:ListItem>天津</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtname" runat="server" />
</div>
</form>
</body>
</html>
这个简单,用AJAX 来做