如何使用C#code用于超链接的onClick事件? [英] How to use the onClick event for Hyperlink using C# code?

查看:504
本文介绍了如何使用C#code用于超链接的onClick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个条件为我在我的页面的超链接。

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

而不是只使用一个特定的链接像:< A HREF =帮助/ Tutorial.html>教程< / A> 我要显示不同的页面对于不同的用户。例如,如果用户登录以管理员身份,他们会比普通用户不同的链路psented $ P $。

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.

我已经修改我的超级链接为:&LT;一的onclick =displayTutorial_Click&GT;&教程LT; / A&GT;

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

和添加了这个$​​ C $ C:

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

推荐答案

哇,你有一个巨大的misanderstanding如何asp.net的作品。

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

这行code的

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

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

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.

有一个非常简单的解决办法是在你的Page_Load方法来改变链接的href attribut。

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

您的aspx code:

Your aspx code:

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

Page_Load中的

您codebehind / CS code:

Your codebehind / cs code of page_load:

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

不要忘了再次检查AdminToturorial.html到prevent黑客的管理员权限。

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

这篇关于如何使用C#code用于超链接的onClick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆