如何使用 C# 代码为超链接使用 onClick 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8784555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:25:00  来源:igfitidea点击:

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

c#asp.net

提问by AlwaysANovice

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.

而不是仅仅使用一个特定的链接,比如:<a href="help/Tutorial.html">Tutorial</a>我想为不同的用户显示不同的页面。例如,如果用户以管理员身份登录,他们将看到与普通用户不同的链接。

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

我已将我的超链接修改为: <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 Tutoriallink work properly? Thank you a lot in advance!!!

但这没有用。谁能帮我弄清楚如何使教程链接正常工作?预先非常感谢您!!!

采纳答案by Grrbrr404

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

哇,你对 asp.net 的工作方式有很大的误解。

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.

不会将管理员用户重定向到新站点,而是在服务器(通常是浏览器、IE)上启动一个新进程并加载该站点。那肯定不是你想要的。

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

一个非常简单的解决方案是更改 page_load 方法中链接的 href 属性。

Your aspx code:

你的aspx代码:

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

Your codebehind / cs code of page_load:

您的 page_load 代码隐藏 / cs 代码:

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

Dont forget to check the Admin rights again on "AdminToturorial.html" to "prevent" hacking.

不要忘记在“AdminToturorial.html”上再次检查管理员权限以“防止”黑客攻击。

回答by prema

this may help you.

这可能对你有帮助。

In .cs page,

在 .cs 页面中,

//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

在 .aspx 时代通过这个变量

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

回答by Jeremy Wiggins

The onclickattribute 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.)

onclick锚标记上的属性将调用客户端函数。(如果您想在单击链接时调用 javascript 函数,您将使用此方法。)

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

您想要的是服务器端控件,例如LinkButton

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

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

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

这有一个OnClick属性,可以调用后面代码中的方法。

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 LinkButtoncontrol in the code behind.

进一步查看您的代码,您似乎只是想根据用户的访问级别打开不同的教程。您根本不需要事件处理程序。更好的方法是LinkButton在后面的代码中设置控件的终点。

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