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.
Related
I found a nice article on how to detect page refresh in this article: Detecting browser 'Refresh' from Code behind in C#, I tried to follow the code and use it in a web form application written in vb.net.
My problem is that when I refresh the page using the browser refresh button, the boolean IsPageRefresh, always remains false. So the code that I am trying to prevent from executing on page refresh, keeps getting executed.
Here is an example if my vb.net code including the converted code from the article:
Dim IsPageRefresh As Boolean = False
Protected Sub Account_reset_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
ViewState("ViewStateId") = System.Guid.NewGuid().ToString()
Session("SessionId") = ViewState("ViewStateId").ToString()
Else
If ViewState("ViewStateId").ToString() <> Session("SessionId").ToString() Then
IsPageRefresh = True
End If
Session("SessionId") = System.Guid.NewGuid().ToString()
ViewState("ViewStateId") = Session("SessionId").ToString()
End If
End Sub
Protected Sub Account_reset_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
If (Not Page.IsPostBack) Then
If Not IsPageRefresh Then
If Not String.IsNullOrEmpty(Request("key")) Then
If Not (Customer.SignInByResetKey(Request("key"))) Then
Customer.Messages.Add("Invalid password reset key", MessageType.Error)
FormsAuthentication.SignOut()
Functions.Redirect(SiteLink.ResetPassword)
Else
' Do Nothing
End If
Else
Functions.Redirect(SiteLink.ResetPassword)
End If
Else
'Do Nothing
End If
End If
End Sub
I would like to ask for help to try to identify where I possibly am making the mistake, or if there is a better method.
I saw many articles that show how to catch page refresh in code behind, but they use the PreRender method, and I cannot implement that solution due to how to website was built.
I hope I can get some positive feedback.
Many thanks.
I am having problem in triggering Msgbox. It is not working in other browser than that of the iis server.
This is my code
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If (Not Session("user") = 1) Then
MsgBox("You are not signed in!")
Exit Sub
End If
End Sub
How to resolve this problem.
Thanks
Since you say in the browser, I assume you're using WinForms or MVC, etc. MessageBox doesn't exist in that context. Look into using Javascript or some client-side code to send a message to the user. Something like this should be close:
String cstext = "You are not signed in!";
Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", cstext, true);
or:
ScriptManager.RegisterStartupScript
Good luck.
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 am using the WebBrowser control in asp.net page. here is the simple code:
Public Class _Default
Inherits System.Web.UI.Page
Private WithEvents browser As WebBrowser
Dim th As New Threading.Thread(AddressOf ThreadStart)
Sub ThreadStart()
browser = New WebBrowser
AddHandler browser.DocumentCompleted, AddressOf browser_DocumentCompleted
browser.Navigate("http://www.someurl.com/")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
th.SetApartmentState(Threading.ApartmentState.STA)
th.Start()
th.Join()
End Sub
Private Sub browser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
If browser.Document IsNot Nothing Then
Dim textbox As HtmlElement = browser.Document.GetElementById("txt1")
textbox.InnerText = "some text"
Dim button As HtmlElement = browser.Document.GetElementById("btn1")
button.InvokeMember("click")
End If
End Sub
End Class
the problem is that the webbrowser's DocumentCompleted event is not being handled. It looks like the page request finishes before anything else could happen.
what's the solution to this problem?
I really recommend reading this article(He won a price for it..)
Using the WebBrowser Control in ASP.NET
http://www.codeproject.com/KB/aspnet/WebBrowser.aspx
His solution is to create 3 threads for it to work..
I'm not sure but I have some concerns about the way you wrote your code.
You are creating and initializing your thread as soon as your class instance is created. This is before the form has been loaded.
I can't say for sure this couldn't work but I would definitely recommend creating the thread in your Load event handler, just before you use it.
I wrote some similar code in C# to generate a website thumbnail. Although that code does not use the DocumentCompleted event, I played with that event when I wrote it and it seemed to work okay. You can compare my code to yours.
Also, I should mention I have one hosting account where the code doesn't work. It seems to simply die when I call Thread.Join. However, it doesn't appear that's the issue you're running into.
I have a web application (.NET 3.5) that has this code in Global.asax:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
LinkLoader()
PathRewriter()
PathAppender()
End Sub
I want all those functions inside to get called except for when it's an AJAX call back. So, ideally I would have it changed to:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
If not Page.IsCallback then
LinkLoader()
PathRewriter()
PathAppender()
End If
End Sub
But there is not access to the page object here. So, basically my question is:
How do I check if the request is an AJAX call back inside Application_BeginRequest?
Thank you very much for any feedback.
John,
Thanks for pointing me in the right direction. The solution is actually to check for Request.Form("__ASYNCPOST"). It is set to "true" if it is a CallBack.
Thanks so much for the help!
You should have access to the HttpContext.Current.Handler object which you can cast to a Page object and get Page.IsPostBack or Page.IsCallBack. Although in order to do this safely you need to first test that it is a Page object and not null:
With HttpContext.Current
If TypeOf .Handler Is Page Then
Dim page As Page = CType(.Handler, Page)
If page IsNot Nothing AndAlso (page.IsCallBack OrElse page.IsPostBack) Then
'Do something
End If
End If
End With
From my understanding all IsCallback does is check if the form has a post variable named __CALLBACKARGUMENT. You could check the form yourself in Context.Request.Form and that should tell you the same thing as IsCallback.