开发者

How do you pass variables from c# to javascript?

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


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.


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;

}


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜