7

On loading the page I want to pass a value to my javascript function from a server side variable.

I cannot seem to get it to work this is what I have:

Asp.Net

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

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

Also I would like to try and accomplish this without using hdden fields

1
  • Just to be certain, the script tag is in the page that has the .aspx.cs containing public string blah { get; set; } declared in it and the page has declared that .aspx.cs file as its code behind right? Commented Jun 13, 2012 at 19:17

6 Answers 6

11

I believe your C# variable must be a class member in order to this. Try declaring it at the class level instead of a local variable of Page_Load(). It obviously loses scope once page_load is finished.

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

    protected void Page_Load(object sender, EventArgs e)
    {
        blah = "ER432";
        //....
Sign up to request clarification or add additional context in comments.

7 Comments

I am getting this error:CS0103: The name 'blah' does not exist in the current context
You must be doing something wrong. Are you doing it similar to my recent edit? I have tested that and it works fine.
Is my javascript right? It says blah is not in current context
Ahhhhh! I had the code in a web form and the js in the master page. It worked fine Thanks
@RyanSammut Not unless the variable is in the master class, I don't think so.
|
3

In this particular case, blah is local to Page_Load you'll have to make it a class level member (probably make it a property) for it to be exposed like that.

Comments

2

You can put a hidden input in your html page:

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

param1.value = "myparamvalue"

Finally your javascript function can access as below:

document.getElementById("param1").value

2 Comments

It's webforms so you'd have to include the runat="server" on your input to make it accessible from server code
You might need to add ClientIDMode=static otherwise the id is adorned with some internal prefixes.
0

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. If you're using 4.0, you can set the hidden field client id mode to static to help keep things cleaner as well. 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.

If you really want to use code--like a variable, it needs to be accessible at the class scope.

Comments

0

Your string blah needs to be a public property of your Page class, not a local variable within 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";
    }
}

1 Comment

This code is not working for me can you please check my javascript, I am getting an error saying "blah" is not in current context
0

You should declare blah at class level.

3 Comments

@Matthew.. thanks for your help, but make sure when answer is single line or less then it should be a comment, hope you got me!
@RDC is there a specific rule that says this? I feel my answer solves the exact problem without inendating the reader with useless information.
@Matthew.. Nope there is no rule like that, even i agree with you that your answer solve the problem so i gave thanks :) , but as you like to help people same way we can make better stack-overflow by managing our Q/A, well..i always Vote up comments those solve the problem or valuable as an answer post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.