Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get global variable's value in jquery?

I've declared a static class named Global. In that class I've declared a static string variable LastID. I'm assigning values to this static variable in different web pages. Now I want to get the value of this Global variable in my .aspx page through jQuery. Can you suggest how I can get the value? In my Global class, code looks like this:

public static class Global
{
    static string lastID;
    public static string ImportantData
    {
        get
        {
            return lastID;
        }
        set
        {
            lastID = value;
        }
    }
}

I'm assigning value like this:

string LID = "MyID";
Global.ImportantData = LID;

Now I want to get this Global.ImportantData value with jQuery. How do I do that?

like image 539
Sukanya Avatar asked Dec 31 '11 07:12

Sukanya


1 Answers

You can do something like:

WebForms:

<script type="text/javascript">
    var importantData = '<%= Global.ImportantData %>';
</script>

Razor:

<script type="text/javascript">
    var importantData = '@Global.ImportantData';
</script>

Be sure to fully qualify the namespace of Global unless you have included it in your Web.Config.

Edit

In response to the comment, you can also assign it to a hidden field and just parse it out with jQuery as well:

ASPX:

<asp:HiddenField ID="ImportantData" runat="server" />
...
<script type="text/javascript">
    var importantData = $("#<%= ImportantData.ClientID %>").val();
</script>

Code behind:

protected void Page_Load (object sender, EventArgs e)
{
    ImportantData.Value = Global.ImportantData;
}
like image 103
Jim D'Angelo Avatar answered Sep 21 '22 18:09

Jim D'Angelo