我想在回发之前先对表单进行客户端验证,所以我正在使用onclientclick事件来验证表单,如果有效,请做一些服务器端的工作。

这是我的代码:

<head runat="server">
    <script type="text/javascript" id="Script1">
        function performCheck() {
            return $("TextBox1").val() == "";
        }
</script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server" >
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">

        </telerik:RadScriptManager>
    <div>
            <asp:TextBox ID="TextBox1" runat="server" CausesValidation="true" ValidationGroup="thisGpo"></asp:TextBox>
        <asp:RequiredFieldValidator EnableClientScript="true"  ID="RequiredFieldValidator1" ValidationGroup="thisGpo" runat="server" ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
        <asp:Button OnClientClick="performCheck()"  ID="Button1" runat="server" Text="Button" ValidationGroup="thisGpo" CausesValidation="true" />     </div>
    </form>
</body>


问题是,如果我使用telerik控件,它们会添加标签

    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>


而且这种形式的地狱导致回发覆盖了我的代码,因此令人沮丧。

我的老板强迫我们使用Telerik,因为他们有这个花哨的numericTextBox,并且他在付款。

试图与TELERIK联系,但他们提到他们无法为我们提供支持,因为我们的版本已过期。

最佳答案

由于要进行客户端验证,因此可以添加“ return false;”。到OnClientClick

<asp:Button OnClientClick="performCheck(); return false;"  ID="Button1" runat="server" Text="Button" ValidationGroup="thisGpo" CausesValidation="true" />


然后要调用服务器端,我建议要么创建一个调用服务器端方法的隐藏按钮,要么使用ajax。

使用隐藏按钮:

<asp:Button runat="server" ID="btnDoTheThing" style="display: none;" OnClick="btnDoThething_Click"></asp:Button>


服务器端代码:

protected void btnDoTheThing_Click(object sender, EventArgs e)
{
            // Do The Thing
}


因此,一旦javascript验证完成并且是有效的,然后使用jQuery像调用这样单击按钮

$("#btnDoTheThing").click();


另外,一旦验证有效,就可以使用Ajax调用服务器端代码。

关于javascript - Telerik asp.net客户端验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32019249/

10-12 13:07
查看更多