We help IT Professionals succeed at work.

Request.QueryString not working in master page

1,851 Views
Last Modified: 2012-05-11
My program uses a master page and also has a login.

If a user attempts to open a certain project via a URL link which includes the project id (pid) in the query string and they are not logged in, they will be redirected to the login page.  What I want is for the user to then be able to login and then be taken to the page initially requested.  

The only problem I am having is that I cannot seem to obtain the part of the query string that I need from when the user enters the URL.  

The code I am using is below -
Partial Class MasterPage
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim pid As String = HttpContext.Current.Request.QueryString("pid")

        If Session("AccessGranted") = True Then
            ASPxMenu1.Visible = True
            FullNameLabel.Text = Session("FullName")
            FullNameLabel.Visible = True
            WelcomeLabel.Visible = True
            LogOffASPxButton.Visible = True
        Else
            ASPxMenu1.Visible = False
            FullNameLabel.Visible = False
            WelcomeLabel.Visible = False
            LogOffASPxButton.Visible = False
        End If

        If (Session("AccessGranted") = False) And Page.GetType().Name <> "login_aspx" Then

            Response.Redirect("~\login.aspx?err=1&pid=" & pid)
    
        End If


    End Sub

Open in new window

Comment
Watch Question

Try the following:

 Response.Redirect("~/login.aspx?err=1&pid=" & pid)

Or,

 Response.Redirect(ResolveUrl("~/login.aspx?err=1&pid=" & pid), True)

Author

Commented:
Thanks although none of them worked.

It seems as if the following line is not picking up the pid correctly and a blank instead -

Dim pid As String = HttpContext.Current.Request.QueryString("pid")

I have also tried -

Dim pid As String = Request.QueryString("pid")
CERTIFIED EXPERT

Commented:
try

 Request.QueryString.Get("pid")
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
the redirect occurs in the master page as well.
can you give an example of the complete url which the user will hit?
OK.  How do you populate the value of "pid" in the first place.  I tried multiple combinations of sending the following test redirect:

Response.Redirect("WebForm2.aspx?pid=test123")

and capture the value in WebForm2.aspx in page load using

Dim pid As String = Request.QueryString("pid")

and it works perfectly.  I tried it in a Master Page (Site.Master) Page Load being used by Content Page WebForm1.aspx and tried it as well using WebForm1.aspx.cs Page Load to redirect to WebForm2.aspx.  Everything works.

So, how do you populate your value of "pid"?  Can you provide additional code snippet to see how you do it?

Author

Commented:
The user will attempt to hit this -

https://projects.mywebsite.com.au/projects/projectdetails.aspx?pid=2862

If they are not logged in it will redirect them to the login page which should be this -

https://projects.mywebsite.com.au/projects/login.aspx?err=1&pid=2862

But instead it is this -

https://projects.mywebsite.com.au/projects/login.aspx?err=1&pid=

Author

Commented:
obtaining pid and the redirect all occurs in the master page as shown in the code above
When the user hit the first link, are you using an HTML anchor tag or are you using an ASP.NET Control such as a HyperLink or LinkButton?  Have you tried to capture the QueryString pid value using one of the events of the control if it is an ASP.NET control?

Author

Commented:
its just a html link in an email that is sent to a user
CERTIFIED EXPERT

Commented:
have you debug and check

Dim pid As String = HttpContext.Current.Request.QueryString("pid")


whether you are getting value here or not ?

Author

Commented:
I have.  I am not getting anything.  

pid = NOTHING
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
OK.  I tried to reproduce it.  I setup a html link in an email automatically and send the email to myself.  Click on the url link with PID information in it.  Captured it in my test website and voila!  I got the PID displayed in another page using a Response.Redirect.

Note:  I used http rather than https.  The secured site must have something to do with the pid value missing.

1

gfgfdfdfgdf
njgh
jukhjh
sorry the last comment happened by mistake!
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks guys, I placed the code in the content page as well and it's all working now.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.