5

I am trying to add a condition for a hyperlink that I have in my page.

Instead of just using a particular link like: <a href="help/Tutorial.html">Tutorial</a> I want to display different pages for different users. For example, if the user is logged in as Admin, they will be presented with different link than regular users.

I have modified my hyperlink as: <a onclick="displayTutorial_Click">Tutorial</a>

and added this code:

    protected void displayTutorial_Click(object sender, EventArgs e)
    {
        // figure out user information
        userinfo = (UserInfo)Session["UserInfo"];

        if (userinfo.user == "Admin")

            System.Diagnostics.Process.Start("help/AdminTutorial.html");

        else

            System.Diagnostics.Process.Start("help/UserTutorial.html");            
    }

But this didn't work. Can anyone please help me to figure out how I can make the Tutorial link work properly? Thank you a lot in advance!!!

3 Answers 3

16

The onclick attribute on your anchor tag is going to call a client-side function. (This is what you would use if you wanted to call a javascript function when the link is clicked.)

What you want is a server-side control, like the LinkButton:

<asp:LinkButton ID="lnkTutorial" runat="server" Text="Tutorial" OnClick="displayTutorial_Click"/>

This has an OnClick attribute that will call the method in your code behind.

Looking further into your code, it looks like you're just trying to open a different tutorial based on access level of the user. You don't need an event handler for this at all. A far better approach would be to just set the end point of your LinkButton control in the code behind.

protected void Page_Load(object sender, EventArgs e)
{
    userinfo = (UserInfo)Session["UserInfo"];

    if (userinfo.user == "Admin")
    {
        lnkTutorial.PostBackUrl = "help/AdminTutorial.html";
    }
    else
    {
        lnkTutorial.PostBackUrl = "help/UserTutorial.html";
    }
}

Really, it would be best to check that you actually have a user first.

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["UserInfo"] != null && ((UserInfo)Session["UserInfo"]).user == "Admin")
    {
        lnkTutorial.PostBackUrl = "help/AdminTutorial.html";
    }
    else
    {
        lnkTutorial.PostBackUrl = "help/UserTutorial.html";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Jeremy for your elaborated answer! however, when I am trying the exact thing, I am getting this error message: "The name 'lnkTutorial' does not exist in the current context" even after adding the ID for the asp:HyperLink. any idea why?
@Jeremy I can't find an OnClick event for the HyperLink control...were you possibly thinking of LinkButton?
5

Wow, you have a huge misunderstanding how asp.net works.

This line of code

System.Diagnostics.Process.Start("help/AdminTutorial.html");

Will not redirect a admin user to a new site, but start a new process on the server (usually a browser, IE) and load the site. That is for sure not what you want.

A very easy solution would be to change the href attribute of the link in you page_load method.

Your aspx code:

<a href="#" runat="server" id="myLink">Tutorial</a>

Your codebehind / cs code of page_load:

...
if (userinfo.user == "Admin")
{
  myLink.Attributes["href"] = "help/AdminTutorial.html";
}
else 
{
  myLink.Attributes["href"] = "help/otherSite.html";
}
...

Don't forget to check the Admin rights again on "AdminTutorial.html" to "prevent" hacking.

1 Comment

Please check answer of Jeremy Wiggins if you want to use the Server Control "HyperLink"
1

this may help you.

In .cs page,

//Declare a string
   public string usertypeurl = "";
  //check who is the user
       //place your code to check who is the user
       //if it is admin
       usertypeurl = "help/AdminTutorial.html";
       //if it is other 
        usertypeurl = "help/UserTutorial.html";

In .aspx age pass this variabe

  <a href='<%=usertypeurl%>'>Tutorial</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.