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.
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.
This is one of those problems which seems like it should have a simple solution but I can't work out what it is!
How can I pass a control from one sub to another if the first sub doesn't actually call the second? For example, where btnChangeText is in a panel that has a ModalPopupExtender called mpExample, and therefore isn't usually visible:
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
<SpecifiedTextBox>.Text = "Hello"
End Sub
And then on the main page, visible at all times, is a button associated with each textbox. In this example, it's textbox15:
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
<Set TextBox15 as variable>
mpExample.Show()
End Sub
I know it's a silly example - believe me when I say that the real application I want to make of this actually makes sense! But the point is that I want to somehow store the name of the control to be updated by the first sub when the second sub is run.
If I was calling the first sub from the second it'd be easy, I'd just pass it as an argument, but I'm not. The first sub is called from a button click and is an independent action from the running of the second sub.
I don't seem to be able to use a session variable (my first thought) because I can't find any way to store the control name as a string and then convert it back to an actual control when the first sub runs. That'd be the easiest answer if somebody could tell me how to do it.
One approach would be to store the control's ID as a string in a Session variable, and then use the FindControl method to grab the control in your 2nd Click event.
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15.ID
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Page.FindControl(Session("currentTextBox")),TextBox)
currentTextBox.Text = "Hello"
End Sub
Note that if your TextBox15 control is inside some kind of container (a Panel or something), you'll need to use that container's FindControl method, rather than Page.FindControl.
Another approach is to store the TextBox itself in a Session variable, and then pull that out to set the text in your other method. Note that this only works if the methods are both called in the same request (which doesn't sound like it would work for your use-case). Here's what that would look like:
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Session("currentTextBox"), TextBox)
currentTextBox.Text = "Hello"
End Sub
I have code in my ASP.NET master Page_Init event page that checks if a user is authorized to be on the content page, and if not redirects them to the login page. This code works fine as far as the check itself.
However, I have discovered that the content Page_Load event still fires after the above redirect.
This is causing a problem on pages that assume the user is logged in and certain variables are set.
This is the master page code (simplified)
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
...
If Access_Level > User_Level_ID Then
Response.Redirect("~/login.aspx", False)
End If
End Sub
The above test works fine, and the redirect line is executed, but doesn't take effect before the code below is fired and executes.
This is the content page code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Rec_IDs As New List(Of String)
Rec_IDs = Session("Rec_IDs")
lblCount.Text = String.Format("You have {0} records in your cart", CType(Rec_IDs.Count, String)) 'this gives an error if Session("Rec_IDs") is null
End Sub
I realize I can put code in each of my content pages to check if a user is logged in / authorized, but I wanted to control it all from one location if possible.
Am I doing something wrong? I've read so many pages that say the master page is the place to do the check.
Thanks. :-)
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
...
If Access_Level > User_Level_ID Then
Response.Redirect("~/login.aspx", True)
End If
End Sub
Using Response.Redirect("~/login.aspx", True) will terminate the current page processing & redirect to desired page.
Though it is recommended to use "Response.Redirect("~/login.aspx", False)" but this will not terminate page execution. It will redirect after current page processing end.
It does so because the 2nd argument in your Response.Redirect is set to false - which means you're not ending the execution of the rest of the page.
If you set it to true, page execution ends (prevents the Page_Load of the content page/s from firing. EDIT: as well as any other subsequent Master page events for that matter)
Response.Redirect("~/login.aspx", True)
Check what it does to all your pages though....e.g. your login.aspx page shouldn't have the same master page the way the code is written above...
Partial Class ClientCenter_UpdateSub
Inherits System.Web.UI.Page
Structure PInfo
Dim Name As String
Dim Surname As String
End Structure
Dim OldPInfo As New PInfo
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
'blah blah
OldPInfo.Name = Dt.Rows(0).Item("Name").ToString
OldPInfo.Surname = Dt.Rows(0).Item("Surname").ToString
end if
end sub
End Class
The first time the page loads my structrure is filled correctly.
After an AJAX postback all the structure fields are setting to nothing. (It seems that the Dim OldPInfo As New PInfo is called again), but i should better ask the SO Experts.
So anyway, what am i doing wrong here?
First off, You should never assign a variable outside of a property or a method.
Second, web applications are stateless (which means NOTHING is automatically saved from call to call - unless you store it somewhere like Viewstate, Session, etc.).
Remember to accept this answer if it helps solve your problem.
Can I get some help posting across different pages from a custom control?
I've created a custom button that raises it's own click event through the following code:
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Const EventName As String = "button_click"
Const ArgName As String = "__EVENTARGUMENT"
If Page.IsPostBack _
AndAlso Request.Params IsNot Nothing _
AndAlso Request.Params(ArgName).Trim = EventName Then
Me.OnClick(Me.this_button, New EventArgs)
Else
Me.this_button.Attributes.Add("OnClick", Page.ClientScript.GetPostBackEventReference(Me.this_button, EventName))
End If
End Sub
How would I go about modifying this to let me post to a different page?
I'd like it to act as close as possible to the System.Web.UI.WebControls.Button property PostBackUrl.
You can use Cross Page Postbacks.
You can also use the WebForm_DoPostBackWithOptions js method to postback the current page to another page.