How to use onClick event for Hyperlink using C # code?

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

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

I changed 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 that did not work. Can someone help me figure out how I can use the Tutorial link correctly? Thank you very much!

+4
source share
3 answers

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

This line of code

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

It will not redirect the administrator user to a new site, but start a new process on the server (usually in a browser, IE) and load the site. This is definitely not what you want.

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

Your aspx code:

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

Your code / cs code for page_load:

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

Do not forget to check the administrator rights to "AdminToturorial.html" again to "prevent" hacking.

+7
source

The onclick attribute of your anchor tag will call the client function. (This is what you would use if you would like to call the javascript function when the link is clicked.)

What you want is a server control, such as 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.

Looking further at your code, it looks like you're just trying to open another tutorial based on the user's access level. You do not need an event handler for this. The best thing would be to simply set the endpoint of your LinkButton control in code.

 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"; } } 

In fact, it would be better to verify that you actually have a user.

 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"; } } 
+14
source

it can help you.

In the .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"; 

At the age of .aspx pass this parameter

  <a href='<%=usertypeurl%>'>Tutorial</a> 
+1
source

Source: https://habr.com/ru/post/1389996/


All Articles