简体   繁体   English

将C#值传递给Javascript

[英]Pass C# Values To Javascript

On loading the page I want to pass a value to my javascript function from a server side variable. 在加载页面时,我想从服务器端变量向我的javascript函数传递一个值。

I cannot seem to get it to work this is what I have: 我似乎无法让它工作这就是我所拥有的:

Asp.Net Asp.Net

protected void Page_Load(object sender, EventArgs e)
{
    string blah="ER432";
}

Javascript 使用Javascript

<script type="text/javascript">

    var JavascriptBlah = '<%=blah%>';

    initObject.backg.product_line = JavascriptBlah;

</script>

Adding this to the page 将其添加到页面

 public string blah { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
           blah="ER432";
        }

I still am getting an error: CS0103: The name 'blah' does not exist in the current context 我仍然收到错误:CS0103:当前上下文中不存在名称'blah'

Also I would like to try and accomplish this without using hdden fields 此外,我想尝试在不使用hdden字段的情况下完成此操作

I believe your C# variable must be a class member in order to this. 我相信你的C#变量必须是一个类成员才能做到这一点。 Try declaring it at the class level instead of a local variable of Page_Load() . 尝试在类级别声明它而不是Page_Load()的局部变量。 It obviously loses scope once page_load is finished. 一旦page_load完成,它显然会失去范围。

public partial class Example : System.Web.UI.Page
{
    protected string blah;

    protected void Page_Load(object sender, EventArgs e)
    {
        blah = "ER432";
        //....

在这个特殊情况下, blahPage_Load本地,你必须使它成为一个类级别的成员(可能使它成为一个属性),让它像那样暴露。

You can put a hidden input in your html page: 您可以在html页面中添加隐藏的输入:

<input  type="hidden" runat='server' id="param1" value="" />

Then in your code behind set it to what you want to pass to your .js function: 然后在您的代码后面将它设置为您想要传递给.js函数的内容:

param1.value = "myparamvalue"

Finally your javascript function can access as below: 最后你的javascript函数可以访问如下:

document.getElementById("param1").value

你应该在班级宣布blah

Your string blah needs to be a public property of your Page class, not a local variable within Page_Load . 你的string blah需要是Page类的公共属性,而不是Page_Load的局部变量。 You need to learn about scoping. 您需要了解范围界定。

public class MyPage
{
    public string blah { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        blah = "ER432";
    }
}

What I've done in the past is what webforms does as it's functionality--creating a hidden field to store values needed by the client. 我过去所做的是webforms所做的功能 - 创建一个隐藏的字段来存储客户端所需的值。 If you're using 4.0, you can set the hidden field client id mode to static to help keep things cleaner as well. 如果您使用的是4.0,则可以将隐藏字段客户端ID模式设置为静态,以帮助保持清洁。 Basically, add you value to the hidden field and then from javascript you can get the value or modify it too(in case you want to pass it back) since it's just a dom element. 基本上,将您的值添加到隐藏字段,然后从javascript您可以获取值或修改它(如果您想要传回它),因为它只是一个dom元素。

If you really want to use code--like a variable, it needs to be accessible at the class scope. 如果你真的想使用代码 - 比如一个变量,它需要在类范围内可访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM