Arizona1911
Arizona1911

Reputation: 2201

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?

Upvotes: 0

Views: 1317

Answers (3)

Jason Kleban
Jason Kleban

Reputation: 20778

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.

Upvotes: 0

BlueCode
BlueCode

Reputation: 741

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

Upvotes: 1

Tim Green
Tim Green

Reputation: 2032

If there's one specific variable you want, you can use the document.location property and split out the part after id=

Upvotes: 0

Related Questions