ASP.NET 文本框 DropDownExtender

发布于 2024-10-19 23:51:36 字数 1038 浏览 5 评论 0原文

如何从下拉列表中获取值返回到文本框? 以下不起作用。不过,您可以从列表中选择该项目。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若有似无的小暗淡 2024-10-26 23:51:36

您需要将 onchange 事件连接到 ListItem 以调用 JavaScript 方法,该方法从所选项目中获取文本。您的 JavaScript 代码将如下所示(未测试):

    function setOptionText()
    {
       var ddl = $get('ListBox1');
       var index = ddl.selectedIndex

       $get('TextBox1').value = ddl.options[index].value;
    }

然后相应地连接您的 ListBox 控件。请注意,您不再需要 AutoPostBack 选项,因为 JavaScript 正在处理设置文本。

<asp:ListBox ID="ListBox1" runat="server" onchange="return setOptionText()">

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):

    function setOptionText()
    {
       var ddl = $get('ListBox1');
       var index = ddl.selectedIndex

       $get('TextBox1').value = ddl.options[index].value;
    }

Then you wire up your ListBox control accordingly. Note that you no longer need the AutoPostBack option since JavaScript is handling setting the text.

<asp:ListBox ID="ListBox1" runat="server" onchange="return setOptionText()">
把昨日还给我 2024-10-26 23:51:36

如果您为列表框的 SelectedIndexChanged 事件添加处理程序,则可以将所选项目的文本放入文本框中:

标记:

<div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    
    <ajaxtoolkit:dropdownextender id="TextBox1_DropDownExtender" dropdowncontrolid="ListBox1"
        runat="server" enabled="True" targetcontrolid="TextBox1"
        highlightbackcolor="WhiteSmoke" />
</div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox_SelectedIndexChanged">
    <asp:ListItem>Item 1</asp:ListItem>
    <asp:ListItem>Item 2</asp:ListItem>
    <asp:ListItem>Item 3</asp:ListItem>
</asp:ListBox>

代码:

protected void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    TextBox1.Text = ListBox1.SelectedItem.Text;
}

但是,如果页面上有许多文本框、扩展器和列表框,那么管理起来可能会出现问题,因此您可以 将所选项目的文本放入文本框中。可能需要考虑将 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:

<div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    
    <ajaxtoolkit:dropdownextender id="TextBox1_DropDownExtender" dropdowncontrolid="ListBox1"
        runat="server" enabled="True" targetcontrolid="TextBox1"
        highlightbackcolor="WhiteSmoke" />
</div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox_SelectedIndexChanged">
    <asp:ListItem>Item 1</asp:ListItem>
    <asp:ListItem>Item 2</asp:ListItem>
    <asp:ListItem>Item 3</asp:ListItem>
</asp:ListBox>

Code:

protected void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    TextBox1.Text = ListBox1.SelectedItem.Text;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文