将 html 生成为文字字符串时,论坛身份验证注销不起作用
我正在尝试通过动态生成的 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 += " | <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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您尝试使用 javascript 客户端事件 (onlick="...") 进行回发。另外,使用 ASP.net 指令 <%= %>在后面的代码中将不起作用。事实上,如果您现在在浏览器上检查渲染的 HTML,您可能会发现以下内容:
无论如何,如果您无法在 aspx 文件中生成此代码,您应该考虑将其替换为 __dopostback javascript 函数( http://stackoverflow.com/questions/3591634/how-to-use-dopostback):
但是,您确实应该考虑以某种方式将所有这些代码放入您的 aspx 文件中。我会这样做:
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:
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):
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: