简体   繁体   中英

Passing value from C# codebehind to javascript

What is the best way to pass a value from C# code to javascript? Currently I am setting an asp.net hidden field in the Page_Load method.

Also if I pass a value using GET like

Response.Redirect("myurl.com/myPage.aspx?id=300");

how can I get the value of id from myPage using javascript?

Is there a nice way to do this in jquery?

function getParameter(name)
{
    name = name.replace(/[[]/,"\[").replace(/[]]/,"\]");
    var results = new RegExp("[\?&]" + name + "=([^&#]*)").exec(window.location.href);

    return (results != null ? results[1] : "");
}

Use the following code to get your parameter: getParameter("id")

如果要使用一个特定的变量,则可以使用document.location属性,并在id=之后分割出一部分

Tim's idea is good. You could also directly insert the values into the javascript with something like var idValue = '<%= SomeProtectedProperty %>'; within your script. That's for if you know it at load time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM