Skip to content Skip to sidebar Skip to footer

Using C# Variable In Javascript

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

Solution 1:

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

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

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

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.

//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.

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

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);
}

Solution 2:

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


Solution 3:

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

var IP = '@HomeController.ServerIP'

where ServerIP is a public static variable in the class HomeController. Make sure to import your class at the top of the cshtml file.


Solution 4:

C#:

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

VB:

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

Post a Comment for "Using C# Variable In Javascript"