Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从codebehind向javascript函数传递变量_C#_Javascript_Function - Fatal编程技术网

C# 从codebehind向javascript函数传递变量

C# 从codebehind向javascript函数传递变量,c#,javascript,function,C#,Javascript,Function,我主要想从codebehind中获取一个变量的值,以便在javascript函数中用于自动完成…函数如下: $(document).ready(function() { $('.PONumbers').autocomplete( { source: function(request, response) { $.ajax({ url: "../GenericH

我主要想从codebehind中获取一个变量的值,以便在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");
    });    

我有值“622”的地方是我想要代码隐藏中的值的地方……有什么建议吗?

尝试在代码隐藏中公开一个属性,您应该能够像这样访问该值:

userid:'<%= this.UserID %>'
使用页面方法

您可能也可以利用
PageMethods
实现这一点:

[ScriptMethod, WebMethod]
public static string GetLabelText()
{
    return "Hello";
}
关于客户:

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

insertLabelData=函数(){
GetLabelText(onSuccess,onFailure);
} 
onSuccess=函数(结果){
var lbl=document.getElementById('lbl');
lbl.innerHTML=result;//代码隐藏值
} 
onFailure=功能(错误){
警报(错误);
} 
以下是我获取上述代码的文章:

下面是另一篇关于
PageMethods
的教程:

尝试在代码隐藏中公开属性,您应该能够访问如下值:

userid:'<%= this.UserID %>'
使用页面方法

您可能也可以利用
PageMethods
实现这一点:

[ScriptMethod, WebMethod]
public static string GetLabelText()
{
    return "Hello";
}
关于客户:

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

insertLabelData=函数(){
GetLabelText(onSuccess,onFailure);
} 
onSuccess=函数(结果){
var lbl=document.getElementById('lbl');
lbl.innerHTML=result;//代码隐藏值
} 
onFailure=功能(错误){
警报(错误);
} 
以下是我获取上述代码的文章:

下面是另一篇关于
PageMethods
的教程:

只需将用户ID作为自定义html属性添加到ui自动完成标记中即可。然后读一读

<input type="text" class="ui-autocomplete" userid="<%=userID%>">

只需将用户ID作为自定义html属性添加到ui自动完成标记中。然后读一读

<input type="text" class="ui-autocomplete" userid="<%=userID%>">

您的代码隐藏使用什么语言?如果请求需要此值,我相信您应该在客户端提供此值(即使您必须将其放入隐藏字段或其他内容),以便您可以轻松地将该值作为请求数据传递。您的代码隐藏使用什么语言?如果请求需要此值,我相信你应该在客户端使用这个值(即使你必须把它放在一个隐藏的字段或其他地方),这样你就可以很容易地将这个值作为请求数据传递。你的意思是只需声明nd inititalise变量,是吗?只需在代码中创建一个返回你需要的值的属性,在本例中,哪个是
用户ID
。您的意思是只需声明并初始化变量,是吗?只需在代码中创建一个返回所需值的属性,在本例中,该属性将是
用户ID
。不确定您在这里的意思,不太熟悉javascriptI。我为您添加了一个示例。这是最简单的方法,因为您不需要更改动态JS文件。不确定您在这里的意思,不太熟悉javascriptI,我为您添加了一个示例。这是最简单的方法,因为您不需要更改动态JS文件。