【问题标题】:ASP.NET add html button click eventASP.NET 添加 html 按钮点击事件
【发布时间】:2014-09-12 23:24:45
【问题描述】:

我必须在asp.net中添加html按钮点击事件。

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

我在这里使用 html 按钮,因为我必须将它放在 asp.net form(runat server) 标记之外。所以我不能在这里使用链接按钮。第二件事是我必须使用图标作为按钮。因此,如果有任何其他方法可以完成这两项任务,请也提出建议。

【问题讨论】:

  • 您可以使用 onclick javascript 事件和 ajax 脚本来完成此任务。
  • 你能给我一个样品吗
  • 在进行 Web 表单开发时,我通常将表单放在正文中。然后,您可以在任何地方使用您的服务器控件。
  • 是的。但在这里我不能这样做
  • 如果它在带有runat=server... 的表单之外,那么只需将按钮包装在它自己的表单中,该表单可以发布到您想要的任何页面。您不需要标准的“点击”事件函数。只需传递一个隐藏的表单变量,然后在您发布到的页面上,如果该变量存在,运行您的函数。

标签: asp.net twitter-bootstrap


【解决方案1】:

在html按钮中:

<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>

在javascript文件中:

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

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    相关资源
    最近更新 更多