开发者

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 javascrip开发者_开发技巧t?

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")


If there's one specific variable you want, you can use the document.location property and split out the part after 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜