Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to apply an onclick() event to a span tag?

Tags:

c#

asp.net

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:

 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's where I call this event:

 <a href="Log_In.aspx"><span class="MenuItem" runat="server" onclick="LogOff_Click">Log Off</span></a>
like image 486
Chris Avatar asked Jan 18 '12 21:01

Chris


People also ask

Can we use Onclick in span tag?

When clicking on the p element, it will return both the p and the div element, since the p element is inside the div element. But when clicking on the span element, it will only return itself, and not the p and the div element (even though its inside these elements).

How do you write onclick events in a tag?

You could write <a href="javascript:window. onclick = location. reload;">Activate me to reload when anything is clicked</a> . Within HTML, onclick can mean something on its own, as long as its part of an HTML tag: <a href="#" onclick="location.

Does Onclick work on any element?

The onclick attribute is part of the Event Attributes, and can be used on any HTML elements.


4 Answers

Standard DOM and CSS will allow this

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

should do the trick

like image 124
Silvertiger Avatar answered Oct 09 '22 12:10

Silvertiger


Your LogOff_Click is fine. However, you need to modify 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>
like image 30
The Vanilla Thrilla Avatar answered Oct 09 '22 13:10

The Vanilla Thrilla


Description

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.

Check out my sample and this jsFiddle Demonstration

Sample for the <div> tag

Html

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

More Information

  • jsFiddle Demonstration
  • w3c shools tag definition
like image 27
dknaack Avatar answered Oct 09 '22 12:10

dknaack


If you wanted to stay in ASP.NET, you could use a 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 Behind

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're still writing JavaScript, but you're staying with the ASP.NET controls and the ASP.NET way of setting client-side events.

I actually prefer the method of dknaack and Silvertiger -- put the event in the client side code (preferably a javascript file instead of inline).

like image 2
David Hoerster Avatar answered Oct 09 '22 12:10

David Hoerster