1

I'm trying to pass a GUID variable via the query string. When I run my game, I can see the needed variable in the querystring when it hits the page(planetID):

http://ec2.us-east-2.compute.amazonaws.com/World_Generation/GeneratePlanet.aspx?planetID=3b757g83-f211-1236-a52c-226fw9a9f9d5

However, when I try to display that variable, I get this error in my browser console window(Chrome):

Uncaught ReferenceError: planetID is not defined

Here is my code. You can see that I am declaring the variable 'planetID' and setting it from the query string value:

Partial Public Class PlanetGallery
    Page.Header.DataBind()
    Public planetID As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not String.IsNullOrEmpty(Request.QueryString("planetID")) Then planetID = Request.QueryString("planetID")

        ScriptManager.RegisterStartupScript(Page, Me.Page.GetType, "test", "alert('planetID from PlanetGallery_PageLoad Sub' + planetID);", True)
    End Sub

End Class

I'm not sure why it's not working. I think I need another pair of eyes.

If anyone has any suggestions, I'd love to hear them!

Thanks!

5
  • where do you get this error? the only error you can get is a javascript error if I see correct Commented Apr 17, 2017 at 14:29
  • @jackjop thanks for asking! I forgot to put that info in there. I've updated my question. I get the error in my browser console window. Commented Apr 17, 2017 at 14:31
  • You're not concatenating planetID. You're not passing the value of your planetID VB variable, you're literally passing the string "planetID" to JavaScript. Commented Apr 17, 2017 at 14:31
  • @Santi I'm sorry, what do you mean? planetID is just a GUID value. Thanks! Commented Apr 17, 2017 at 14:33
  • It should be: "alert('planetID from PlanetGallery_PageLoad Sub" & planetID & "');" - If you want the actual value of your variable to get passed, it can't be within quotes. The javascript that you're actually passing in your example is: alert('planetID from PlanetGallery_PageLoad Sub' + planetID); - By passing the word planetID instead of its value, the JavaScript thinks it's a variable. Commented Apr 17, 2017 at 14:34

1 Answer 1

1

Since you're getting the error at browser console, you should change your code like this:

ScriptManager.RegisterStartupScript(Page, Me.Page.GetType,
      "test", "alert('planetID from PlanetGallery_PageLoad Sub" + planetID + "');", True)

The problem is because javascript cannot access to codebehind variables.

Your javascript is registered like this:

<script>
      alert("planetID from PlanetGallery_PageLoad Sub " + planetID);
</script>

So planetID becomes a variables this way.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.