[英]Passing a variable to javascript function from codebehind
i bascially want to get the value of a variable from the codebehind to use in a javascript function for autocomplete....here is the function: 我基本上想从代码隐藏中获取变量的值,以用于自动完成的javascript函数中。...这是该函数:
$(document).ready(function() {
$('.PONumbers').autocomplete(
{
source: function(request, response) {
$.ajax({
url: "../GenericHandlers/PONumber.ashx",
dataType: "json",
data: {
q: request.term,
userid:'622'
},
success: function(data) {
response(data);
}
});
},
minLength: 3
})
}).unbind("blur.autocomplete");
$("body:not(.ui-autocomplete)").live('click', function(){
$('.PONumbers').autocomplete("close");
});
Where i have the value '622' is where i want the value from the codebehind....any suggstions? 我的值为“ 622”的地方是我想要隐藏代码中的值的地方。...任何建议吗?
Try exposing a property in the code-behind, and you should be able to access the value like this: 尝试在后面的代码中公开一个属性,您应该能够访问如下所示的值:
userid:'<%= this.UserID %>'
And in the code-behind: 并在后面的代码中:
public int UserID
{
get
{
return (int)Session["UserID"];
}
}
Using Page Methods 使用页面方法
You could probably leverage PageMethods
for this too: 您可能也可以利用
PageMethods
来PageMethods
这一点:
[ScriptMethod, WebMethod]
public static string GetLabelText()
{
return "Hello";
}
And on the client: 在客户端上:
<script type="text/javascript">
insertLabelData = function() {
PageMethods.GetLabelText(onSuccess, onFailure);
}
onSuccess = function(result) {
var lbl = document.getElementById('lbl');
lbl.innerHTML = result; //your code-behind value
}
onFailure = function(error) {
alert(error);
}
</script>
Here's the article I took the above code from: 这是我从以上代码中获取的文章:
http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/04/asp-net-ajax-pagemethods.aspx http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/04/asp-net-ajax-pagemethods.aspx
And here is another tutorial on PageMethods
: 这是有关
PageMethods
另一篇教程:
http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX
Just add the user ID as custom html properties in your ui-autocomplete tag. 只需在ui-autocomplete标签中将用户ID添加为自定义html属性即可。 Then read from it.
然后从中读取。
<input type="text" class="ui-autocomplete" userid="<%=userID%>">
Then your Javascript you can get this attr: 然后,您可以使用Javascript获得以下属性:
$(".ui-autocomplete").attr('userid') // will return the id printed in your input attr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.