简体   繁体   English

从DropDownList选择ASP.NET/Javascript填充文本框

[英]Populate a TextBox from a DropDownList selection ASP.NET/Javascript

I have a DDL and ASP.NET Textbox. 我有一个DDL和ASP.NET文本框。 I would like to populate the text box with the option I choose from the DDL. 我想用从DDL中选择的选项填充文本框。 I need this to be instant and not use postbacks so it would seem JavaScript would be the obvious choice here. 我需要做到这一点是即时的,并且不使用回发,因此JavaScript在这里似乎是显而易见的选择。 I have done quite a bit of searching but everything I have found seems to be for standard HTML (Selects and Inputs) and these do not appear to work with ASP objects: 我已经做了很多搜索,但是发现的所有内容似乎都是针对标准HTML(选择和输入)的,而这些似乎不适用于ASP对象:

<asp:DropDownList runat="server" ID="DDLSalesPerson" DataValueField="keyid" DataTextField="FullName" />

<asp:TextBox runat="server" id="txtSalesPerson" />

My DDL is populated from SQL in the code-behind page. 我的DDL是从SQL的代码隐藏页中填充的。

Can somebody assist with the appropriate code I should use? 有人可以协助我使用适当的代码吗? Thanks. 谢谢。

ASP.Net controls render as standard HTML elements on the browser. ASP.Net控件在浏览器上呈现为标准HTML元素。 In script, you can get a reference to them by using the ClientID property of the ASP.Net control. 在脚本中,您可以使用ASP.Net控件的ClientID属性获得对它们的引用。

Put this in a script block in your aspx: 将其放在aspx的脚本块中:

var ddl = document.getElementById('<%=DDLSalesPerson.ClientID %>');
var textBox = document.getElementById('<%= txtSalesPerson.ClientID%>');

Now you have references to the DOM objects for the select and input elements that the ASP.Net controls rendered and you can use the techniques you've already learned on the HTML elements. 现在,您可以引用ASP.Net控件呈现的select和input元素的DOM对象,并且可以使用已经在HTML元素上学到的技术。

Additional info You need to add an onchange attribute to your DropDownList control as such: 其他信息您需要像这样将一个onchange属性添加到DropDownList控件中:

<asp:DropDownList runat="server" ID="DDLSalesPerson" DataValueField="keyid" onchange="ddlChange();" DataTextField="FullName" />

and then put this script block in your aspx 然后将此脚本块放入aspx

<script type="text/javascript">

    function ddlChange()
    {
        var ddl = document.getElementById('<%=DDLSalesPerson.ClientID %>');
        var textBox = document.getElementById('<%= txtSalesPerson.ClientID%>');

        textBox.value = ddl.options[ddl.selectedIndex].text;
    }

</script>

As you change the dropdown list, you'll see the textbox update. 更改下拉列表时,您会看到文本框更新。 Tested in IE and Chrome. 在IE和Chrome中进行了测试。

Since you've pointed out that you're a JavaScript beginner, may i suggest you use an updatepanel control. 既然您已经指出您是JavaScript初学者,那么我是否建议您使用updatepanel控件。 An updatepanel allows you to execute server code without refreshing the page. 使用updatepanel无需刷新页面即可执行服务器代码。 Simply place the dropdownList and the textbox in the same updatepanel or in two separate updatepanels and write the code for the textbox to update based on the dropdownlist selection. 只需将dropdownList和文本框放在相同的updatepanel或两个单独的updatepanel中,然后为文本框编写代码即可根据dropdownlist选择进行更新。 Make sure to set the dropdownlist to do autopostback. 确保将下拉列表设置为自动回发。 The asp markup is as follows: asp标记如下:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlList" runat="server" 
        AutoPostBack="True">
        <asp:ListItem>-select-</asp:ListItem>
        <asp:ListItem>option 1</asp:ListItem>
        <asp:ListItem>option 2</asp:ListItem>
    </asp:DropDownList>
<asp:TextBox ID="txtTextBox" runat="server" />

</ContentTemplate> 
</asp:UpdatePanel> 

The codebehind in vb is as follows: vb中的代码如下:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If ddlList.Text <> "-select-" then
           txtTextBox.Text = ddlList.text
        End If
    End Sub

If you're new to JavaScript, use the jQuery library (simply provide references to the CDN-hosted jQUery files at google.com) and then you can use the following code: 如果您不熟悉JavaScript,请使用jQuery库(只需在google.com上提供对CDN托管的jQUery文件的引用),然后可以使用以下代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
    $("#DDLSalesPerson").change(function () {
        $("#txtSalesPerson").val($(this).val());
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javascript填充ASP.NET下拉列表 - populate ASP.NET dropdownlist using javascript 在 asp.net 中使用 javascript 填充国家和城市下拉列表 - Populate countries and cities dropdownlist with javascript in asp.net ASP.NET - 丢失通过javascript填充的下拉列表选项 - ASP.NET - losing selection of dropdownlist populated via javascript 使Javascript代码在ASP.Net中的DropDownList上的选择更改时触发 - Getting Javascript code to fire on a selection change on a DropDownList in ASP.Net 如何使用JavaScript将值从父asp.net下拉列表传递到弹出框中的文本框 - How to pass value from parent asp.net dropdownlist to textbox in popup using javascript 如何使用ASP.NET WebForms中的JavaScript从FormView中的DropDownList获取文本到TextBox中? - How to get text into a TextBox from a DropDownList in FormView using JavaScript in ASP.NET WebForms? 如何在Asp.net中使用Javascript根据DropDownlist中的数据选择设置标签文本属性 - How to set Label Text property based on data selection from DropDownlist using Javascript in Asp.net 禁用基于下拉选择asp.net javascript的启用文本框 - disable enable textbox based on dropdown selection asp.net javascript 从ASP.NET文本框中使用JavaScript - Using javascript from an ASP.NET textbox ASP。 当下拉列表发生变化时,使用javascript网络填充下拉列表 - asp. net populate dropdownlist using javascript when a dropdownlist is change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM