To make sure that an event handler is written properly, I generally have Visual Studio generate the event for me. However, I can't find a way to do this with a div and I've tried typing it out myself to no avail. Is this even possible without writing any Javascript? (I saw similar questions, but couldn't seem to find anything that fit my needs).
Edit: Basically I have a logoff div disguised to the user as a button. When they click it, I want the following to happen:
编辑:基本上我有一个注销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();
}
Your LogOff_Click is fine. However, you need to modify your markup. Apply the onserverclick event to the tag instead of . In your case, try the following:
The div element supports the Javascript event onclick so you can do it. You can check if a html element supports a given event by looking on w3c shools tag definition.
It is not clear to me what you exactly mean. You can do many things using Javascript on the client side. onclick is Javascript but you can do things like redirects using the serverside (Postback on ASP.NET Webforms) too. Things you do with Javascript are, without doing ajax, not noticeable by the server, cause Javascript get handled by the browser.
Not sure what exactly you intend to do. When the onclick event is triggered, you would need a handler to be called, when the event is triggered. This is done via Javascript.
Answering the original question, onclick event can be triggered via
tag. Make sure you test against Internet Explorer 7/8 (compatibility mode) as not all events work in IE 7/8. It should be okay in Firefox, Chrome and IE9. Save this in a file and experiment.
回答原始问题,可以通过
标签触发onclick事件。确保您针对Internet Explorer 7/8(兼容模式)进行测试,因为并非所有事件都在IE 7/8中运行。它应该可以在Firefox,Chrome和IE9中使用。将其保存在文件和实验中。