23

Looking to pass variables from c# to javascript to use some jquery code. Passing doubles, ints, strings, arrays. Does anyone know how to do this?

for example if I have this code snip in c#:

string blah = "this is a blah string";

I would like to pass this into javascript so that I could use a mouseover event in jquery:

$('#myDiv').mouseover(function(){ //do something with my 'blah' string });

5 Answers 5

17

Nevermind I think I figured it out. Assuming the code above, you can write in javascript:

<script type="text/javascript"> var JavascriptBlah = '<%=blah%>'</script>

This will pass the blah in c# to the JavascriptBlah on the client side. Then you can manipulate on client side.

Sign up to request clarification or add additional context in comments.

2 Comments

The downside to this is that if "blah" has a single-quote in it, you're in trouble. There's sense in converting your string to JSON first before passing it.
Does anyone know if using "<%=VariableName%>, is functional if the variable "blah" is being assigned a value with a "private async Task Foo()" I have it being assigned in a private async task and I'm wondering if that's the reason why it's not working for me.
4

You can use public properties on the code behind as well as the session. Here is a sample using public properties that can be read from a Javascript function.

Front End:

function IsAgentInProgram()
{
    var optStatus = "<%=AgentOptInStatus%>";

    if (optStatus == "True")
        alert("You are opted in!");
    else
        alert ("You are opted OUT");
}

Code Behind:

public bool AgentOptInStatus;

private void Page_Load(object sender, System.EventArgs e)
{
    this.AgentOptInStatus = true;

}

Comments

2

I recently used this ToJson() extension method along with Page.RegisterClientScriptBlock Method to pass down a multi-level mapping object that populates three levels of cascaded dropdowns.

2 Comments

not sure what you mean here, but could you see above for the edited portion?
WARNING Page.RegisterClientScriptBlock is obsolite since Framework v.4.0. ClientScriptManager.RegisterClientScriptBlock method is now recommended.
1

you can use asp:HiddenField variables to pass values between codebehind and js.. this accomplished with viewstate of page during postbacks...

1 Comment

stackoverflow.com/questions/252222/… the link has a sample code by this way you may access value of hiddenfield either code behind or javascript
1
<script>
    const blah = @Json.Serialize(Model.Title);
</script>

This uses IJsonHelper.Serialize, which JSON-encodes characters such as double quotes and angular brackets. This is secure in the sense that it does not allow HTML tags. It is correct in the sense that all characters end up the same in the JavaScript variable.

E.g.

const blah = @Json.Serialize("John's</script>");

turns into

const blah = "John\u0027s\u003c/script\u003e";

The default implementation for IJsonHelper uses System.Text.Json.Serializer with a HTML-safe encoder. The NewtonSoft.Json implementation also encodes HTML correctly.

@Html.Raw is not needed since Json.Serialize returns an IHtmlContent that is not HTML-encoded.

Comments

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.