ASP.NET 文本框 DropDownExtender
如何从下拉列表中获取值返回到文本框? 以下不起作用。不过,您可以从列表中选择该项目。
<body>
<form id="form1" runat="server">
<script type="text/javascript">
function pageLoad() {
//Same Width
$get('ListBox1').style.width = $get('TextBox1').clientWidth;
}
</script>
<ajax:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajax:ToolkitScriptManager>
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<ajax:DropDownExtender ID="TextBox1_DropDownExtender" DropDownControlID="ListBox1"
runat="server" DynamicServicePath="" Enabled="True" TargetControlID="TextBox1"
HighlightBackColor="WhiteSmoke">
</ajax:DropDownExtender>
</div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
</asp:ListBox>
</form>
How to get the value from the dropdown to return to the TextBox?
The following does not work. You can select the item from list though.
<body>
<form id="form1" runat="server">
<script type="text/javascript">
function pageLoad() {
//Same Width
$get('ListBox1').style.width = $get('TextBox1').clientWidth;
}
</script>
<ajax:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajax:ToolkitScriptManager>
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<ajax:DropDownExtender ID="TextBox1_DropDownExtender" DropDownControlID="ListBox1"
runat="server" DynamicServicePath="" Enabled="True" TargetControlID="TextBox1"
HighlightBackColor="WhiteSmoke">
</ajax:DropDownExtender>
</div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
</asp:ListBox>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将 onchange 事件连接到 ListItem 以调用 JavaScript 方法,该方法从所选项目中获取文本。您的 JavaScript 代码将如下所示(未测试):
然后相应地连接您的 ListBox 控件。请注意,您不再需要 AutoPostBack 选项,因为 JavaScript 正在处理设置文本。
You'll want to wire up the onchange event to your ListItem to call a JavaScript method that gets the text from the item that was selected. Your JavaScript code would look something like this (not tested):
Then you wire up your ListBox control accordingly. Note that you no longer need the AutoPostBack option since JavaScript is handling setting the text.
如果您为列表框的 SelectedIndexChanged 事件添加处理程序,则可以将所选项目的文本放入文本框中:
标记:
代码:
但是,如果页面上有许多文本框、扩展器和列表框,那么管理起来可能会出现问题,因此您可以 将所选项目的文本放入文本框中。可能需要考虑将 TextBox、Extender 和 ListBox 全部包装在 UserControl 中。
You can put the selected item's text into the TextBox if you add a handler for the ListBox's SelectedIndexChanged event:
Markup:
Code:
However, this could get really problematic to manage if you have a number of TextBoxes, Extenders and ListBoxes on the page so you might want to consider wrapping the TextBox, Extender and ListBox all up together in a UserControl.