Solved

Redirect from Page_Load Event

Posted on 2004-08-13
4
260 Views
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 asp.net 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
      conn.Open()
      objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
      While objDR.Read()
         intID=objDR("UserName")
         strFirst=objDR("Company")
         strLast=objDR("Email")
         NameFirst=objDR("FirstName")
           NameLast=objDR("LastName")
         strPhone=objDR("Phone")
         strExtension=objDR("Extension")
         strRole=objDR("Role")
         End While
      page.databind()
      conn.Close()

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

 IncidentProduct.Items.Clear()
                     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.Clear()
                     IncidentStatus.Items.Add(New ListItem("Open"))
                     IncidentStatus.Items.Add(New ListItem("Completed"))

IncidentPriority.Items.Clear()
                 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
0
Comment
Question by:nhorsley
4 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
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:

Sub_PagLoad(......)
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
 'else
 'show the login form again

End Sub

Hope this will help, nauman
0
 
LVL 17

Accepted Solution

by:
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
Response.Redirect("Admin.aspx")
Else
redirect to wherever or build more user handling.

Regards,

Aeros
0
 
LVL 8

Expert Comment

by:daffodils
ID: 11796725
Modify your Page_Load as ...

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

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

     IncidentProduct.Items.Clear()........
     .............
End If
End Sub
0
 
LVL 1

Author Comment

by:nhorsley
ID: 11796856
Thank you.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now