如何将asp TextBox值转换为html输入标记

时间:2016-12-13 19:01:15

标签: c# html asp.net

我想知道如何从TextBox “AddressTexBox”获取值并将其设置为输入标记“txtAutocomplete”的值。任何帮助都是极好的。谢谢!!!

<input type="text" id="txtAutocomplete"  name ="txtauto" style="width: 900px" onkeypress = "fillAddress" />&nbsp&nbsp&nbsp&nbsp
    <asp:Label ID="AddressVerifyLabel" Text=" Verify Address: " runat="server"></asp:Label>
    <asp:TextBox ID="AddressTextBox" Columns="20" MaxLength="20" Width="600" runat="server" OnTextChanged ="fillAddress" AutoPostBack="True">
</asp:TextBox>

2 个答案:

答案 0 :(得分:0)

一种方法是使用带有onchange事件

的javascript来完成此操作
<input type="text" id="txtAutocomplete"  name ="txtauto" style="width: 900px"  />
<input type="text" id="AddressTextBox" onchange="myFunction()"  name ="txtauto" style="width: 900px"  />

<script>
function myFunction() {
    document.getElementById("AddressTextBox").value = document.getElementById("txtAutocomplete").value ;
}
</script>

另一个是在后端使用c#

<input type="text" id="txtAutocomplete" runat = "server"  name ="txtauto" style="width: 900px" onkeypress = "fillAddress" />

OnTextChanged事件

txtAutocomplete.Value = AddressTextBox.Text;

答案 1 :(得分:0)

目前还不是很清楚你想做什么,所以我会提供2个选项。而asp:TextBoxinput type="text"在HTML中是相同的。查看页面源代码并亲自查看。

首先是使用AddressTextBox事件从txtAutocomplete获取OnTextChanged的值。要使其工作,您必须使txtAutocomplete成为一个aspnet TextBox。

<asp:TextBox ID="txtAutocomplete" runat="server"></asp:TextBox>
<asp:TextBox ID="AddressTextBox" runat="server" OnTextChanged="fillAddress" AutoPostBack="True"></asp:TextBox>

然后在代码背后

protected void fillAddress(object sender, EventArgs e)
{
    txtAutocomplete.Text = AddressTextBox.Text;
}

第二种选择是使用JavaScript,正如@Usman所暗示的那样。除非fillAddress中有更多内容发生,否则这是更好的解决方案,因为它不执行PostBack,从而节省了往返服务器的往返。但是我仍然建议你让txtAutocomplete成为一个aspnet TextBox。

<asp:TextBox ID="txtAutocomplete" runat="server"></asp:TextBox>
<asp:TextBox ID="AddressTextBox" runat="server" onkeydown="myFunction()"></asp:TextBox>

<script type="text/javascript">
    function myFunction() {
        document.getElementById("<%= txtAutocomplete.ClientID %>").value = document.getElementById("<%= AddressTextBox.ClientID %>").value;
    }
</script>

请注意<%= txtAutocomplete.ClientID %>的使用。 Aspnet将控件的ID更改为此类ctl00$mainContentPane$AddressTextBox。因此,如果您使用document.getElementById("AddressTextBox").value JavaScript将找不到该元素并抛出错误。

相关问题