在javascript中访问C#变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/665995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:30:17  来源:igfitidea点击:

Accessing C# variable in javascript

c#asp.netjavascript

提问by

I'm going to design a website for DMS (educational domain) in C#.NET, which I am new to.

我将在 C#.NET 中为 DMS(教育领域)设计一个网站,这是我的新手。

In one of my ASPX pages, I want to disable a menu, which is in JavaScript, according to accessright.

在我的一个 ASPX 页面中,根据accessright.

The accessrightstored in database table loginas one attribute in SQL server, and I want to retrieve that accessrightto one C# variable and want to access that variable in JavaScript.

accessright存储在数据库表login中的SQL服务器一个属性,我希望检索accessright到一个C#变量,要访问,在JavaScript变量。

If there is another possible approach please tell.

如果有另一种可能的方法,请告诉。

回答by Canavar

You can not access server side variables in client-side code. Only way is to use an AJAX library or to inject your variables to page as client side script.

您不能在客户端代码中访问服务器端变量。唯一的方法是使用 AJAX 库或将您的变量作为客户端脚本注入页面。

Inject your variable's value to page like that :

像这样将变量的值注入页面:

string script = string.Format("var myVariable = '{0}';", vaiable.ToString());
if (!ClientScript.IsClientScriptBlockRegistered("myScript"))
{
    ClientScript.RegisterClientScriptBlock(typeof(_Default), "myScript", script, true);
}

and in your javascript functions use myVariableas your server side variable.

并在您的 javascript 函数中使用myVariable作为您的服务器端变量。

But I thing you should start with Client Side or Server Sideconcept.

但我认为你应该从客户端或服务器端的概念开始。

回答by Gustavo

This is a sample code I quickly googled. http://www.eggheadcafe.com/community/aspnet/7/63763/using-c-variable-in-java.aspxIt should point you in the right direction. strvariable is your C# variable.

这是我快速搜索的示例代码。http://www.eggheadcafe.com/community/aspnet/7/63763/using-c-variable-in-java.aspx它应该指向正确的方向。strvariable 是您的 C# 变量。

    <INPUT id="hd" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 144px" type="hidden"  value="<%=strvariable %>">

<script language="javascript">
function Button_Click()
{
    alert(document.getElementById('hd').value); 
}
</script>

回答by Binoj Antony

Well there is one shortcut we used to use during the ASP days, this still works in ASP.NET
In the page codebehind declare a public property say public int MyInteger
In the aspx put this

嗯,在 ASP 时代我们曾经使用过一个快捷方式,这在 ASP.NET 中仍然有效在
页面代码隐藏中声明一个公共属性说public int MyInteger
在 aspx 中把这个

<script>
    function GetMyValue()
    {
     var someVar = <%=this.MyInteger%>;
     //Do something
    }
</script>

回答by plinth

In our code base we implemented a technology called RemoteInvoke, whereby javascript can call methods on the server without a postback. Effectively, it is call-by-name with dynamic binding on the server side.

在我们的代码库中,我们实现了一项名为RemoteInvoke的技术,借此 javascript 可以在没有回发的情况下调用服务器上的方法。实际上,它是在服务器端具有动态绑定的按名称调用。

The name of the method and its javascript arguments get bundled up and passed to the server, the server unrolls them and finds the best matching method to call. The rules are that you can only call methods with the RemoteInvokable attribute and the allowable data types are numbers, strings and booleans. The code that handles the call delegation will match on signature doing approximate matching where possible (ie, if you pass a floating point number as an arg, it will favor a method with that name and signature, but if there is none, it will find one with a matching integral argument.

方法的名称及其 javascript 参数被捆绑并传递给服务器,服务器展开它们并找到要调用的最佳匹配方法。规则是您只能调用具有 RemoteInvokable 属性的方法,并且允许的数据类型是数字、字符串和布尔值。处理调用委托的代码将匹配签名,并在可能的情况下进行近似匹配(即,如果您将浮点数作为参数传递,它将支持具有该名称和签名的方法,但如果没有,它将找到一个具有匹配的整数参数。

Accessing a server side variable can be implemented through RemoteInvoke.

访问服务器端变量可以通过 RemoteInvoke 实现。

回答by Elham Es

I found this during my problem with passing server silde variable to a Javascript variable. The best way to do this is using eval()javascript method:

我在将服务器 silde 变量传递给 Javascript 变量的问题中发现了这一点。最好的方法是使用eval()javascript 方法:

function GetMyValue(){      
    eval("var someVar = " + parseInt<%=this.MyInteger%>"));
    //Do something     
} 

Use it and enjoy it. ;)

使用它并享受它。;)