为了确保事件处理程序编写正确,通常我会让Visual Studio为我生成事件。但是,我无法找到一种在div元素上生成事件的方法,即使我自己输入代码也不行。是否可以在不编写任何JavaScript的情况下实现这一点?(我看到了类似的问题,但似乎没有符合我的需求的内容)。
编辑:基本上,我有一个伪装成按钮的注销div。当他们点击它时,我希望发生以下操作:
编辑:基本上,我有一个伪装成按钮的注销div。当他们点击它时,我希望发生以下操作:
protected void LogOff_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Session.Abandon();
//This will clear the authentication cookie
HttpCookie myHttpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
myHttpCookie.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(myHttpCookie);
//This will clear the session cookie (not required for my application but applying to be safe)
HttpCookie myHttpCookie2 = new HttpCookie("ASP.NET_SessionId", "");
myHttpCookie2.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(myHttpCookie2);
FormsAuthentication.RedirectToLoginPage();
}
这里是我调用事件的地方:
<a href="Log_In.aspx"><span class="MenuItem" runat="server" onclick="LogOff_Click">Log Off</span></a>
onclick
仍然会执行 JavaScript(无论是内联还是函数调用)。 - David Hoerster