2016-12-13 89 views
0

我想知道如何從文本框「AddressTexBox」獲得的價值,並將其設置爲輸入標籤「txtAutocomplete」的價值。任何幫助都是極好的。謝謝!!!如何獲得ASP文本框的值轉換成HTML input標籤

<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> 
+0

無論何時出現文本更改,將「fillAddress」函數(它是一個c#函數)添加到輸入「txtAutocomplete」中一樣有用 –

回答

0

一個方法是用onchange事件

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

用JavaScript來做到這一點,另一個是是用C#做在後端

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

OnTextChanged事件

txtAutocomplete.Value = AddressTextBox.Text; 
0

你想做什麼不是很清楚,所以我會提供2個選項。而asp:TextBoxinput type="text"在HTML中是一樣的。查看頁面源代碼並親自查看。

首先使用OnTextChanged事件將值從AddressTextBox改爲txtAutocomplete。爲了這個工作,你將不得不使txtAutocomplete一個aspnet文本框。

<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將無法找到該元素並引發錯誤。

相關問題