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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With