Redirect from Page_Load Event

Posted on 2004-08-13
Last Modified: 2006-11-17
I am attempting to use the page load event to choose which aspx page a user finally receives.  I know roles based is desgned to handle this process but I think I can do it this way much easier.  

I have a field in the User table called Role.  There are two roles admin and user.  On Page_Load I want to take the Users Role information and use that to redirect the user to a particular aspx page.   Following is the present code in the page-load event.

How will I edit that code to allow the redirect?

Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then

      Dim strConn as string = "server=(local);database=BCA;Trusted_Connection=yes"
      Dim sql as string = "SELECT UserName, FirstName, LastName, Phone, Extension, Email, Company FROM Users WHERE

(UserName = '" + Session("Username") +"')"
      Dim conn as New SQLConnection(strConn)
      Dim Cmd as New SQLCommand(sql, conn)
      Dim objDR as SQLDataReader
      While objDR.Read()
         End While

(What would the code look like and where would I put the code that says something like:
If strRole=Admin
redirect to Admin.aspx
load the code that follows)

                     IncidentProduct.Items.Add(New ListItem(""))
                     IncidentProduct.Items.Add(New ListItem("Clinic Management System", "CMS"))
                     IncidentProduct.Items.Add(New ListItem("Electronic Medical Records", "EMR"))
                     IncidentProduct.Items.Add(New ListItem("Managed Care Managed Information System", "MCMIS"))
                     IncidentProduct.Items.Add(New ListItem("Resource Scheduling System", "RSS"))

                     IncidentStatus.Items.Add(New ListItem("Open"))
                     IncidentStatus.Items.Add(New ListItem("Completed"))

                 IncidentPriority.Items.Add(New ListItem(""))                    
                 IncidentPriority.Items.Add(New ListItem("Multiple Users Affected"))
                     IncidentPriority.Items.Add(New ListItem("Workaround Indentified, +1 Users Affected"))
                 IncidentPriority.Items.Add(New ListItem("Limited Functionality"))
                 IncidentPriority.Items.Add(New ListItem("Specific Immediate-Need Question"))
                 IncidentPriority.Items.Add(New ListItem("General How-To Question"))

End If
End Sub
Question by:nhorsley
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 25

Expert Comment

ID: 11793985
How you validate the user role? If it depends on a Button_Click event then you can use the page redirect in the following way:

If not Page.IsPostBack then
  'Check if the authentication cookie if you are creating one exist, if exist redirect user to the specific page
  'else show the login form
end if

End Sub

Sub Button_Click(....)
 'Validate User
 'If user exist, redirect to specific page
 'show the login form again

End Sub

Hope this will help, nauman
LVL 17

Accepted Solution

AerosSaga earned 250 total points
ID: 11794029
Put this after the while in your page load and it will auto redirect based on credentials

if not Page.IsPostBack And strRole=Admin then
redirect to wherever or build more user handling.



Expert Comment

ID: 11796725
Modify your Page_Load as ...

Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then

     Dim strConn........
     if strRole=Admin then
         ' Response.Redirect("Admin.aspx")
         ' You can use either a Server.Transfer or Response.Redirect..
         ' Server.Transfer saves an extra round-trip to the server.
     ' load the code that follows
     end if

End If
End Sub

Author Comment

ID: 11796856
Thank you.

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS Deployment problem 5 126
disable data migrations in visual studio 2017 4 69
Securing WEBAPI on Azure 2 56
e commerce steps shown instead of self testing 2 58
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question