Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting JS var with queryString value 2

Status
Not open for further replies.

copeZero

Programmer
Aug 31, 2007
46
CA
Hi why doesn't this work in the front page (not code behind page)?

<script language="javascript">
var tempASP = <%=Request.QueryString["Riskid"];%>;
...
</script>

Thanks
 
I've found the best way to do what you are trying to do is use RegisterClientScriptBlock to build the javascript function from your server side code. I don't do a whole lot of ASP.net coding though, you might want to try forum855

I have some code I will post later (on a different computer)

If you find any other way to do this I would be interested to know.

Hope this helps,

Alex

[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
Something like this, in your page load event

Code:
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine("<script language = Javascript> ");
        sb.AppendLine("<!--");
        sb.AppendLine("function alertMe(){");
        sb.AppendLine("window.open('Default.aspx?Riskid=3500', 'NewWindow', 'width=360,height=410');");
        sb.AppendLine("alert('It worked!' + tempASP);");
        sb.AppendLine("}");
        sb.AppendLine("// -->");
        sb.AppendLine("</script>");
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "onload", sb.ToString());
        sb = null;

I'm sure over simplified, but you get the picture :)

HOpe it helps,

Alex

[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
Page.Request is not public. A property/function must be public to access from the markup. it would need to look like this.
code behind
Code:
public class MyPage : Page
{
   public int RiskId
   {
      get 
      {
         return int.Parse(Request.QueryString["Riskid"]);
      }
   }
}
markup
Code:
<script language="javascript">
   var tempASP = <%=RiskId;%>;
</script>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Nice Jason, I didn't think of that!

However, since the query string is not going to change, wouldn't it be better to 'hard code' the value in your javascript when the page loads, just so you don't have to keep going back to the server every time you use your js?

I'm not trying to be difficult, but coming from a windows background I don't understand some of the web stuff so well (but I'm trying :) )

Also, I just realized I posted the wrong code from the example I threw together last night [blush]

Code:
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine("<script language = Javascript> ");
sb.AppendLine("<!--");
sb.AppendLine("function alertMe(){");
sb.AppendLine("var tempASP = " + Request.QueryString["RiskID"].ToString() + ";"
sb.AppendLine("alert('It worked!' + tempASP);");
sb.AppendLine("}");
sb.AppendLine("// -->");
sb.AppendLine("</script>");
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "onload", sb.ToString());
sb = null;

Hope it helps,

Alex

[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
both of our methods will execute each time the page is returned to the server. unless your's is wrapped in a [tt]if (!Ispostback) { }[/tt].

my code above wouldn't produce additional postbacks to the server. it would be rendered to the client as:
Code:
<html>
   <head>...</head>
   <body>
      <script language="javascript">
         var tempASP = 1;
      </script>
   </body>
</html>
(or whereever the script block is located.)

with all the bells & whistles with asp.net it's still just produces markup (html, js, css). in it's crudest form, asp.net generates a string which is sent to the client. the browser interperets that string. it has no idea whether the string was created by asp.net, php, java, or cold fusion.

This is one reason why viewstate is a blessing and a curse.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
It is wrapped in a ! ispostback. But I am sure that any advantage to this would be minimal at best...

Thanks for the explaination Jason. It was very helpful to me :)



[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top