[英]Autocomplete jquery in asp.net, source from code behind
雖然這個論壇中有很多jquery自動完成代碼。 但是,我沒有找到適合我的SharePoint / ASP.NET網頁案例的任何內容。 我按照jquery自動完成鏈接 。 這很有幫助。 但請看一下
我的代碼:
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("asp:TextBox#TextBox3").autocomplete({
source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
});
});
</script>
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<table>
<tr><td>
<asp:Label ID="Label4" runat="server" Text="Qode"></asp:Label></td><td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td></tr>
</table>
題
1. Can I use the code ` $("asp:TextBox#TextBox3")?`
2. If the source comes from code behind instead of hard copy strings, how to do it?
讓我們在代碼背后說:
string[] source = new string[5];
source[0] = "c++";
source[1] = "java";
source[2] = "php";
source[3] = "coldfusion";
source[4] = "javascript";
那么如何將數組傳遞給jquery代碼呢? 非常感謝。
關於您的第一個問題,如果您使用的是ASP.net版本4或更高版本,則可以將文本框的ClientIDMode設置為“static”,它將強制服務器將ClientID呈現為與服務器ID相同。 然后你可以在你的jQuery代碼中正常引用它。
防爆。
<asp:TextBox ID="TextBox3" runat="server" ClientIdMode="static">
`
$("#TextBox3") // select your text box with standard jQuery id selector
如果您使用的是舊版本的ASP.net,則可以將服務器端代碼插入.aspx以訪問生成的動態clientID。 您也可以將它添加到您的jQuery選擇器中,但它會有所不同。
$("#<%= Textbox3.ClientID %>")
這應該為瀏覽器呈現正確的客戶端ID並由jQuery處理。
至於你的第二個問題,我個人將數組序列化為JSON並將其發送給jQuery。 一個好的庫就是Json.Net( http://json.codeplex.com/ ),其中有一些如何使用該庫的例子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.