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!
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.
Related
I am attempting to call a javascript function on a particular page from my global.asax file, as demonstrated below. I have read in other forums that this is possible using the method below. However, I am struggling to get this to work. I am not receiving any errors in the console, however the function is simply not executing. The function does work as expected and can be called from the page on which it is created without any issues. Any assistance would be greatly appreciated.
Sub Application_AcquireRequestState(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim currentPage = HttpContext.Current.Request.Url.ToString()
If Not currentPage.ToLower().Contains("Online/default.aspx") Then
If HttpContext.Current.Session("UserID") Is Nothing Then
Dim MyPage As System.Web.UI.Page
MyPage = HttpContext.Current.Handler
MyPage.ClientScript.RegisterClientScriptBlock(MyPage.GetType(), "MyScriptKey", "function f(){closeWin(); Sys.Application.remove_load(f);}Sys.Application.add_load(f);", True)
Session.Abandon()
Response.Redirect("~/")
End If
End If
Catch ex As Exception
End Try
End Sub
I have two aspx web pages. In the first one I have this code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Redirect("~/Code.aspx")
End Sub
Now in the Code.aspx page I have this code:
Label1.Text = Request.UrlReferrer.ToString
I want the label show the first page URl, but there is a runtime ERROR.How to fix this? thanks
This is the error message: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Take the redirect out of the page load and put it on a link or something other than a response.redirect. Then try something like this in your code to make sure it's not null:
if(Request.UrlReferrer != null)
{
Label1.Text = Request.UrlReferrer.ToString();
}
else
{
Label1.Text = "No URL referrer";
}
Sorry just noticed you're using VB.net, the code is easy enough to change though and the theory is the same.
If you require the response.redirect I suppose a cookie based solution would work but seems a little involved for such a basic requirement.
I'm not quite sure how to explain this, but I need to be able to add a user's code to the end of the url of each page they go to.
Let me walk you through this.....I am a user, go to my site and type in my user code - which is NOT a login. There is no password, in theory there will be a box or some type of control that will allow a user to enter their code in.
This control will then add that user's code to the end of each URL for every page in the site and will also auto-populate all of the products in our database that are tied to that code.
The user is like a business or affiliate marketer. We need that code at the end of each URL that way when he sends his clients emails with links in them for purchasing, the client will click the link, and that marketer will get credit for the sale(s).
If this makes sense, will someone direct me to a helpful tutorial or explain how this works to me please? Thank you
I was told to use this code:
<ul id="p7menubar">
<li>Home</li>
<li>Lookup
<ul>
<li>Product</li>
<li>Feature</li>
<li>Description</li>
</ul>
</li><!--end trigger li-->
<div id="ib">
Enter your IB code here:
<asp:TextBox ID="IBTextBox" runat="server"></asp:TextBox>
<asp:Button ID="IBTextBoxButton" runat="server" Text="Submit" />
</div>
</ul><!--end p7menubar-->
Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Private _IB As String
Public Property IB() As String
Get
Return _IB
End Get
Set(ByVal value As String)
_IB = value
End Set
End Property
Public Function GetIB(ByVal url As String) As String
If (url.Contains("?")) Then
Return "&IB=" & _IB
Else
Return url & "?IB=" & _IB
End If
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not (String.IsNullOrEmpty(Request.QueryString("IB"))) Then
_IB = Request.QueryString("IB")
End If
End Sub
End Class
UPDATE: With the help of #DJ Quimby and ASP.net 3.5 for Dummies, I am now using this code and it seems to be taking it whatever value I type into my textbox:
Protected Sub IBTextBoxButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles IBTextBoxButton.Click
Session("IB") = IBTextBox.Text
Dim IB As Integer = Session("IB")
Response.Redirect("IBDefault.aspx?Baccount=" + Session("IB"))
End Sub
UPDATE:
<a target="_blank" href="<%# eval("Data") %>?IB=(Session("IB"))"><%#Eval("Title")%></a>
The "IB" gets underlined and says it must be followed by an equal (=) sign and a value. Or if in quotation marks, they must match.
Unless there are any private data concerns, it may be best to store the user's code into session.
You can add most any variable you want to the session by doing the following:
Session("userCode") = "12"
And retrieve it like this:
'Dim userCode as Integer'
'userCode = Session("userCode")'
Just make sure that the type you store it as is compatible with the type you assign it to when you pull it back down.
To check on each page you could declare a private variable within the page to store the value:
Private userCode As Integer
And then
Sub Page_Load(sender As Object, e As EventArgs)
userCode = Session("userCode")
End Sub
Then just execute whatever other code you need to using that value.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lab As Label = Me.GridView1.FindControl("Label1")
If TextBox2.Text = "7" Then
GridView1.SelectedRow.Cells(2).Text = "500"
Else
GridView1.SelectedRow.Cells(2).Text = "950"
End If
End Sub
The following error occurs : Object reference not set to an instance of an object.
You've got this code in your Page Load event, so it will run when the page is first loaded, and on every single postback. This probably isn't what you want.
I imagine that on the very first load, there isn't a selected row in your GridView, so GridView1.SelectedRow is going to be null. If this isn't null, then Cells or Cells(2) definitely will be. Trying to access a property on null is going to throw a NullReferenceException - "Object reference not set to an instance of an object".
As this MSDN example shows, you're probably better off accessing the SelectedRow property in an event handler for the SelectedIndexChanged event of the GridView.
Dim lab As Label = Me.GridView1.FindControl("Label1")
It doesn't look like you're doing anything with this label. Put a breakpoint on that line and see if it finds it. If it doesn't and you don't even use it, take the line out.
Also, check if textbox2 is valid whilst debugging.
I know this has been asked before. Yes, i did my research but it doesn't seem to be working for me so i hope you experts can help me out :)
Here's what my Project looks like
http://i.stack.imgur.com/nnPZJ.png
Yes, the Build Action is Embedded Resource. I also added this in the AssemblyInfo
Assembly: WebResource("WFL.WebResource.EXT.XXX.png", "image/png")
So now, in the default.aspx i say
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cs As ClientScriptManager = Page.ClientScript
Dim strReturn As String = cs.GetWebResourceUrl(Me.GetType(), "WFL.WebResource.EXT.XXX.png")
Dim strReturn2 As String = cs.GetWebResourceUrl(GetType(_Default), "WFL.WebResource.EXT.XXX.png")
Image1.ImageUrl = "http://localhost" + strReturn
Image2.ImageUrl = "http://localhost" + strReturn2
Response.Write("http://localhost" + strReturn)
Response.Write("http://localhost" + strReturn2)
End Sub
But when accessing the returned URL, i get The resource cannot be found.
What am i doing wrong?
Thanks in advance.
This being VB and not C#, you don't need the .EXT portion in WFL.WebResource.Ext.XXX.png. That is something C# needs (to specify folder paths), but VB doesn't. You just want your namespace then resource. Try WFL.WebResource.XXX.png and see if that works.