javascript 在javascript中使用c#变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3921633/
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-10-25 02:41:52  来源:igfitidea点击:

using c# variable in javascript

c#javascript

提问by user471987

I want to know how can I use a C# variable in javascript?

我想知道如何在 javascript 中使用 C# 变量?

回答by Laramie

If I understand your intent, try this in your ASPX page

如果我理解您的意图,请在您的 ASPX 页面中尝试此操作

<script type="text/javascript">
<!--
function showCSharpVar() {
   alert('<%= myCSharpVariable  %>');
}
-->
</script>

where myCSharpVariable is a public variable declared in the code-behind page like this

其中 myCSharpVariable 是在代码隐藏页面中声明的公共变量,如下所示

public string myCSharpVariable = "display me";

you could also make a public property on the page if you need to accomplish something more involved.

如果您需要完成更多涉及的事情,您还可以在页面上创建一个公共属性。

Another option is to embed script directly into your page using either RegisterClientScriptBlock()or RegisterStartupScript(). These are the criteria I use to decide which is best for the situation.

另一种选择是使用 RegisterClientScriptBlock() 或 RegisterStartupScript() 将脚本直接嵌入您的页面。这些是我用来决定哪种情况最适合的标准。

//A. 
//  Page.RegisterClientScriptBlock() will insert the *block* of script just after <form>. 
//  Page.RegisterStartupScript() will insert the script at end of <form>. 
//B. 
//  Page.RegisterClientScriptBlock() should usually be used for scripts encapsulated in functions. (hence the word "block") 
//  Page.RegisterStartupScript() can be used for any script, even if it's  not in a function. 
//C. 
// Page.RegisterClientScriptBlock() should be used for functions that don't need to run on Page load. 
// Page.RegisterStartupScript() should be used for scripts that must run on Page Load. 
//D. 
//  Page.RegisterClientScriptBlock() should be used for a script that does not require the form elements to have been created. 
//  Page.RegisterStartupScript() should be used for scripts that require the form elements to have been created and uses references to them. 
//

With this method, you can inject JavaScript into the page (for example, the array you asked about in the comment), but it makes for often difficult to trace bugs and you will not be able to unit test.

使用这种方法,您可以将 JavaScript 注入页面(例如,您在评论中询问的数组),但是通常很难跟踪错误,并且您将无法进行单元测试。

If you are using UpdatePanels, you might need to use one or the other depending on whether it is a full or partial postback. Try this to get around the issue

如果您使用的是 UpdatePanels,则可能需要使用其中一个,具体取决于它是完整回发还是部分回发。试试这个来解决这个问题

if (ScriptManager.GetCurrent(referenceControl.Page).IsInAsyncPostBack)
{

    ScriptManager.RegisterClientScriptBlock(referenceControl, referenceControl.GetType(), "uniqueID",
        "Your Script Here", true);
}
else
{
    ScriptManager.RegisterStartupScript(referenceControl, referenceControl.GetType(), "uniqueID",
        "Your Script Here", true);
}

回答by Dr. Rajesh Rolen

<script type="text/javascript">

function GetValue() {
var myJsVar = "<%= anyC#Variable %>";

//perform you desired operations.

//执行你想要的操作。

 }
</script>

but remember access specifier of variable "anyC#Variable" should be higher than private. you can take "protected" or "public" but not private.

但请记住变量“anyC#Variable”的访问说明符应该高于私有。您可以选择“受保护”或“公共”,但不能选择私人。

回答by bgs

C#:

C#:

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "<script language='javascript'>alert(" + abc + ");</script>", false)

VB:

VB:

ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "temp", "<script language='javascript'>alert(" + abc + ");</script>", False)

回答by wdavies973

For ASP.NET MVC, I got the following to work:

对于 ASP.NET MVC,我得到了以下工作:

var IP = '@HomeController.ServerIP'

var IP = '@HomeController.ServerIP'

where ServerIPis a public staticvariable in the class HomeController. Make sure to import your class at the top of the cshtmlfile.

其中ServerIPpublic staticHomeController 类中的一个变量。确保在cshtml文件顶部导入您的类。