Can I apply the onclick () event to a span tag?

To make sure the event handler is spelled correctly, I usually have Visual Studio to create an event for me. However, I cannot find a way to do this with the div, and I tried to print it myself to no avail. Is this even possible without writing javascript? (I saw similar questions, but could not find anything that suited my needs).

Edit: Basically, I have an exit logo disguised as a button for the user. When they click on it, I want the following to happen:

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(); } 

Here where I fire this event:

  <a href="Log_In.aspx"><span class="MenuItem" runat="server" onclick="LogOff_Click">Log Off</span></a> 
+6
source share
5 answers

Your LogOff_Click is fine. However, you need to change your markup. Apply the onserverclick event to the <a> tag instead of <span> . In your case, try the following:

  <a href="Log_In.aspx" runat="server" onserverclick="LogOff_Click"><span class="MenuItem">Log Off</span></a> 
+6
source

Description

The div element supports the onclick javascript event so you can do this. You can check if the html element supports this event by looking at the definition of the w3c shools tag .

I don’t understand what exactly you mean. You can do a lot of things using client side javascript. onclick is javascript, but you can do things like redirects using serveride (Postback on ASP.NET web formats). What do you do with javascript without making ajax not visible by the server, make the browser handle javascript.

See my example and jsFiddle Demo

<div> tag example

Html

 <div onclick="alert('hello');">hello world</div> 

Additional Information

+6
source

If you want to stay in ASP.NET, you can use the Panel control and do something like this:

Markup

 <body> <form id="form1" runat="server"> <div> <asp:Panel runat="server" ID="pan1">click here</asp:Panel> </div> </form> </body> 

Code for

 public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { pan1.Attributes.Add("onclick", "javascript:alert('here');return false;"); } } 

You are still writing JavaScript, but you are left with ASP.NET controls and the ASP.NET way to set events on the client side.

I really prefer the dknaack and Silvertiger methods - I put the event in the client code (preferably a javascript file instead of the built-in one).

+2
source

Standard DOM and CSS will allow this.

 <div style="cursor:hand;" onclick="this.document.location.href ='http://www.google.com';"> My content </div> 

gotta do the trick

0
source

Not sure what exactly you intend to do. When the onclick event is fired, the handler that will be called will be needed to call. This is done through Javascript.

You can submit the form in JavaScript or call the Ajax callback in Javascript to interact with your .NET application.

Answering the original question, the onclick event can be fired using the <div> . Make sure you are testing Internet Explorer 7/8 (compatibility mode), as not all events work in IE 7/8. This should be fine in Firefox, Chrome, and IE9. Save it in a file and experiment.

 <html> <head> <script type="text/javascript"> function JSFn() { //you can use the div ID if needed: var id = document.getElementById('div_id'); alert("JSFn was called"); //Can do form submit or Ajax callback to interact with .NET app } </script> </head> <body> <div> <a id="anchor1" href="javascript:JSFn();">Make a clickable link here, you can use onclick and href='#' </a> </div> <div id="div_id2" onclick="JSFn()">Just click here </div> </body> </html> 
0
source

Source: https://habr.com/ru/post/906346/


All Articles