你想做什麼不是很清楚,所以我會提供2個選項。而asp:TextBox
和input 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將無法找到該元素並引發錯誤。
無論何時出現文本更改,將「fillAddress」函數(它是一個c#函數)添加到輸入「txtAutocomplete」中一樣有用 –