将 html 生成为文字字符串时,论坛身份验证注销不起作用

发布于 2024-11-19 15:39:55 字数 1002 浏览 7 评论 0原文

我正在尝试通过动态生成的 HTML(字符串)创建一个注销按钮。

htmlGen = "<div class=\"issue\" style=\"float:right; width:70%;\">";
htmlGen += "Welcome, " + Page.User.Identity.Name.ToString() + " (<a href=\"#\" id=\"LogOutLink\" onclick=\"<%=LogOut_OnClick%>\"> logout </a>)";

正如您所看到的,注销方法位于服务器代码中:

 public void LogOut_OnClick(object sender, EventArgs args)    {
          FormsAuthentication.SignOut();

            string htmlGen = "<div class=\"issue\" style=\"float:right; width:45%;\">";
            htmlGen += "<a style=\"color:White;\" href=\"/Login.aspx\">Login</a>";
            htmlGen += "&nbsp;|&nbsp;<a style=\"color:White;\" href=\"/Accounts/SignUp.aspx\">Register</a>";
            this.Literal1.Text = htmlGen + "</div>";

           FormsAuthentication.RedirectToLoginPage();


   }

无论我做什么,这似乎都不起作用(不会触发,不会注销等)。将 HTML 字符串作为文字执行此操作是否会导致此问题?

有人介意告诉我该怎么做吗?

谢谢!

I am trying to create a logout button through dynamically generated HTML (string).

htmlGen = "<div class=\"issue\" style=\"float:right; width:70%;\">";
htmlGen += "Welcome, " + Page.User.Identity.Name.ToString() + " (<a href=\"#\" id=\"LogOutLink\" onclick=\"<%=LogOut_OnClick%>\"> logout </a>)";

As you can see, the logout method is in the server code:

 public void LogOut_OnClick(object sender, EventArgs args)    {
          FormsAuthentication.SignOut();

            string htmlGen = "<div class=\"issue\" style=\"float:right; width:45%;\">";
            htmlGen += "<a style=\"color:White;\" href=\"/Login.aspx\">Login</a>";
            htmlGen += " | <a style=\"color:White;\" href=\"/Accounts/SignUp.aspx\">Register</a>";
            this.Literal1.Text = htmlGen + "</div>";

           FormsAuthentication.RedirectToLoginPage();


   }

No matter what i do, this doesn't seem to work (won't fire, won't log out, etc). Is there something about doing this as an HTML string to a literal that is causing this?

Would somebody mind telling me how to do this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2024-11-26 15:39:55

首先,您尝试使用 javascript 客户端事件 (onlick="...") 进行回发。另外,使用 ASP.net 指令 <%= %>在后面的代码中将不起作用。事实上,如果您现在在浏览器上检查渲染的 HTML,您可能会发现以下内容:

onclick="<%=LogOut_OnClick%>"

无论如何,如果您无法在 aspx 文件中生成此代码,您应该考虑将其替换为 __dopostback javascript 函数( http://stackoverflow.com/questions/3591634/how-to-use-dopostback):

[...] htmlGen += "onclick=\"__dopostback('" + LogOutButton.ID + "')\""; [...]

但是,您确实应该考虑以某种方式将所有这些代码放入您的 aspx 文件中。我会这样做:

Welcome, <%= UserName %> (<asp:LinkButton ID="LogoutButton" Text="Logout" runat="server"/>)

First, you are trying to do a post back using a javascript client side event (onlick="..."). Also, using ASP.net directive <%= %> in the code behind won't work. In fact, if you check right now in your rendered HTML on your browser, you probably have something along these lines:

onclick="<%=LogOut_OnClick%>"

Anyway, if you can't generate this code in your aspx file, you should consider replacing this by the __dopostback javascript function (http://stackoverflow.com/questions/3591634/how-to-use-dopostback):

[...] htmlGen += "onclick=\"__dopostback('" + LogOutButton.ID + "')\""; [...]

However, you should really consider putting all this code inside your aspx file in a way or another. Here's how I would do it:

Welcome, <%= UserName %> (<asp:LinkButton ID="LogoutButton" Text="Logout" runat="server"/>)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文