ASP.net按钮onserverclick唯一没有被定义的onclick工作时 [英] ASP.net button onserverclick only works when onclick isn't defined

查看:295
本文介绍了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>

据提交表单,但不改变按钮文本点击。文字只是改变为点击是没有定义的onclick()的时候,虽然形式依然提交!

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 =返回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天全站免登陆