ASP.net 按钮 onserverclick 仅在未定义 onclick 时有效 [英] ASP.net button onserverclick only works when onclick isn't defined

查看:14
本文介绍了ASP.net 按钮 onserverclick 仅在未定义 onclick 时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    protected void btnNext_Click(object sender, EventArgs e)
    {
        btnNext.InnerHtml = "CLICK";
    }

    <button type="submit" runat="server" onserverclick="btnNext_Click" id="btnNext">Next &gt;</button>

这很好用,但是当我添加一个 onclick 事件时:

This works fine, but when I add an onclick event:

<button type="submit" runat="server" onserverclick="btnNext_Click" onclick="return checkForm();" id="btnNext">Next &gt;</button>

在头脑中:

<script type="text/javascript">

    function checkForm() {

        if (document.getElementById("<%=lstChooseSpec.ClientID %>").value) {
            return true;
        } else {
            $.jGrowl("<strong>Warning!</strong><br />Please select an item", { sticky: true });
            return false;
        }
    }
</script>

它提交表单,但不会将按钮文本更改为CLICK".尽管表单仍在提交,但文本仅在 onclick() 未定义时更改为CLICK"!

It submits the form, but doesn't change the buttons text to 'CLICK'. The text only changes to 'CLICK' when the onclick() isn't defined, although the form is still submitting!

推荐答案

你看到这个的原因是因为当你在一个按钮上同时运行你的点击处理程序,然后是 __doPostBack 函数 所以 onclick 看起来像这样

The reason you're seeing this is because when you have both on a button it runs your on click handler followed by the __doPostBack function so the onclick would look something like this

onclick="return checkForm(); __doPostBack('btnNext','')

如您所见,__doPostBack 从未被调用,它实际上设置了要调用的服务器函数.然而,由于按钮是提交类型的,表单仍然被发送回服务器.

So as you can see the __doPostBack, which actually sets up which server function to call, is never called. However, since the button is of type submit, the form is still sent back to the server.

这篇关于ASP.net 按钮 onserverclick 仅在未定义 onclick 时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆