简体   繁体   中英

onclick not working when onclientclick is used

Design:

<telerik:RadButton ID="RadBtnSubmit" Text="Submit" runat="server" 
            BackColor="#3399FF" ForeColor="White" 
            Height="30px" Width="100px" ButtonType="LinkButton" AutoPostBack="false"
            OnClientClick="return validate();" 
            UseSubmitBehavior="false" 
            OnClick="RadBtnSubmit_Click"/>

javascript:

    <telerik:RadCodeBlock ID="RadCodeBlock2" runat="server">
    <script type="text/javascript" language="javascript">
        function validate() {
            var CompanyName = /^[A-Za-z0-9]{3,20}$/;
            var txtCompanyName = document.getElementById('<%=radtxtCompanyName.ClientID %>').value;
            if (!CompanyName.test(txtCompanyName)) {
                document.getElementById("<%=radtxtCompanyName.ClientID %>").style.borderColor = "red";
                 alert("false");
                return false;
            }
            alert("true");
            return true;
        }
    </script>
    </telerik:RadCodeBlock>

This button is present inside an update panel. On Client Click alone is working and i am getting alerts correctly according to the condition.
But my onclick part is alone not getting executed.
I also tried putting return false along with function name in onclientclick that also did not work.

Thanks in advance Friends.

set

OnClientClick="if(!validate()) return;"

If you set

OnClientClick="return validate() ;"

It always return no matter your validation is passed or not, and the post-back never triggered.

remove AutoPostback="false" and UseSubmitBehavior="false" from your aspx page. It will work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM