Dushan
Dushan

Reputation: 138

ASP.NET add html button click event

I have to add html button click event in asp.net.

<button type="button" data-toggle="tooltip" data-title="Logout" class="btn btn-link">
<em class="fa fa-sign-out text-muted"></em>
</button>  

I'm using html button here because I have to place this outside of the asp.net form(runat server) tag. So I can't use link button here. The second thing is I have to use icon as button. So if there is any other methods to accomplish both of this tasks please suggest them also.

Upvotes: 0

Views: 432

Answers (1)

user3806621
user3806621

Reputation: 288

In html button:

<button type="button" data-toggle="tooltip" data-title="Logout" class="btn btn-link" onclick="runLogout()">
<em class="fa fa-sign-out text-muted"></em>
</button>

In javascript file:

var xmlHttp = null;

function getXmlHttp() {
    try {
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {
                alert(e.description);
            }
        }
    }
}

function runLogout() {
    getXmlHttp();

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    // ... your code here ...
                }

                return;
            }
        };

    xmlHttp.open("POST", "Logout.aspx", true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.send(null);
}

Upvotes: 0

Related Questions