Link to home
Start Free TrialLog in
Avatar of bidgadget
bidgadgetFlag for United States of America

asked on

asp.net hyperlink

I am building a website where my users login and get directed to the default.aspx page.  I now need to create a conditional hyperlink that will direct the user to a specific page based on their user id

example.  User1  logs in and clicks on the hyperlink and gets linked to page B
                 User2 logis in and clicks and gets directed to page C...
Avatar of rawinnlnx9
rawinnlnx9
Flag of United States of America image

Use your login routines to determine which user you have then test for which user

If (currentUser == "UserA"){
    httpContext.Current.Response.Redirect("pageA.aspx");
    httpContext.ApplicationInstance.CompleteRequest();
}
elseif (currentUser == "UserB"){
    httpContext.Current.Response.Redirect("pageB.aspx");
    httpContext.ApplicationInstance.CompleteRequest();
}
else{
   //return the user to login page:
   httpContext.Current.Response.Redirect("Login.aspx");
   httpContext.ApplicationInstance.CompleteRequest();
}

Open in new window

Avatar of bidgadget

ASKER

Great thank you.  Last question.  Me being new to this.  Where do I actually place the code on the form?
this code will be in your button click event in your code-behind i.e. .cs file

Here it is in VB only slightly different:

If currentUser = "UserA" Then
    HttpContext.Current.Response.Redirect("pageA.aspx")
    HttpContext.ApplicationInstance.CompleteRequest()
ElseIf currentUser = "UserB" Then
    HttpContext.Current.Response.Redirect("pageB.aspx")
    HttpContext.ApplicationInstance.CompleteRequest()
Else
   'return the user to login page:
   HttpContext.Current.Response.Redirect("Login.aspx")
   HttpContext.ApplicationInstance.CompleteRequest()
End If

Open in new window

Thank you.  Do I have to define currentUser.  Im getting errors.  I placed a button on the form and but the code behind it.

I am getting   "currentuser is not declared.

I am using VB
Okay, I can tell you are very new to this. If you designed your page using the designer it should have at least 2 labels for username and password and two textboxes for the user to type that information into. Additionally it should have at least on command button for login. If you double-click any of those controls in the designer it will take you to the code and put you inside the event you most likely need for that code. Ideally you would double-click your command button for the login validation. Inside that click event you would test as an example:

If  txtUsername.Text = "UserA" Then
--- Insert snippit I gave you.
ElseIf txtUsername.Text = "UserB" Then
---Insert appropriate part of snippit I gave you.
Else
---Insert final snippit I gave you.
End If
Actually I apologize.  I am not refering to the login page.  This is after the user is logged in and brought to the default page for all users.  Then I want them to click on a hyperlink and be directed to a different link based on their user id
Well yes. That's just a string and if you just copy and paste the code I gave you are going to have problems.

You'll need to drag a label onto the form and call it something appropriate for it's ID. Then drag a textbox next to the label and call it txtUsername or something similar. Do this for the password field to and call it txtPassword.

Then if you want you could do something like

Dim currentUser as string
currentUser = txtUserName.Text


But currentUser will not exist until you declare it.

Like this:

Dim currentUser As String = txtUsername.Text

If currentUser = "UserA" Then
    HttpContext.Current.Response.Redirect("pageA.aspx")
    HttpContext.ApplicationInstance.CompleteRequest()
ElseIf currentUser = "UserB" Then
    HttpContext.Current.Response.Redirect("pageB.aspx")
    HttpContext.ApplicationInstance.CompleteRequest()
Else
   'return the user to login page:
   HttpContext.Current.Response.Redirect("Login.aspx")
   HttpContext.ApplicationInstance.CompleteRequest()
End If

Open in new window

thank you so much.  I will give it a try and accept your solution.  much appreciated
ASKER CERTIFIED SOLUTION
Avatar of rawinnlnx9
rawinnlnx9
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
question.  Why does it seem I am putting a username and password field on the form.  At this point the user is already validated and at this form.  I just need to then route them using a hyperlink based on their user ID.

Please forgive me if I am not understanding.

Thank you so much
thank you