Solved

asp.net hyperlink

Posted on 2011-02-16
12
686 Views
Last Modified: 2012-05-11
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...
0
Comment
Question by:bidgadget
  • 6
  • 5
12 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34912149
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

0
 

Author Comment

by:bidgadget
ID: 34912162
Great thank you.  Last question.  Me being new to this.  Where do I actually place the code on the form?
0
 
LVL 10

Expert Comment

by:himanshut
ID: 34912180
this code will be in your button click event in your code-behind i.e. .cs file

0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34912186
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

0
 

Author Comment

by:bidgadget
ID: 34912200
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
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34912202
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
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:bidgadget
ID: 34912218
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
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34912223
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

0
 

Author Comment

by:bidgadget
ID: 34912235
thank you so much.  I will give it a try and accept your solution.  much appreciated
0
 
LVL 9

Accepted Solution

by:
rawinnlnx9 earned 500 total points
ID: 34912250
Okay, so we are going way outside the scope of your question. I've answered it sufficiently. For the rest of this you'll need to ask a new question.

You'll need to pass the logged in user as a parameter in a querystring.

So you would as an example:
HttpContext.Current.Response.Redirect("Login.aspx&UserID=" txtUsername.Text.Trim, false)

Open in new window

Then in your destination page you'll have to request that query string using other methods that are part of the HttpContext and then you can use that value to determine where to send them. I've already given way more than your question merrited for a correct response.
0
 

Author Comment

by:bidgadget
ID: 34912262
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
0
 

Author Closing Comment

by:bidgadget
ID: 34912276
thank you
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video discusses moving either the default database or any database to a new volume.

746 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

10 Experts available now in Live!

Get 1:1 Help Now