Jquery验证引擎无法使用ASP.net更新面板

时间:2015-11-19 08:02:43

标签: jquery asp.net jquery-validation-engine

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:MultiView ID="MultiView1" runat="server">
                <asp:View ID="View1" runat="server">
                    <div class="box">
                        <div class="head">
                            <h1>
                                Currency</h1>
                        </div>
                        <div class="inner">
                            <table style="width: 100%">
                                <tr>
                                    <td style="width: 20%">
                                        <asp:Button ID="btnv1AddNew" runat="server" CssClass="btn-success" Text="Add New"
                                            OnClick="btnv1AddNew_Click" TabIndex="1" />
                                    </td>
                                    <td style="width: 50%" align="right">
                                        Currency Name:
                                    </td>
                                    <td style="width: 20%">
                                        <asp:TextBox ID="txtv1CurrencyName" runat="server" Width="80%" CssClass="control-textbox"
                                            TabIndex="2" AutoPostBack="True" OnTextChanged="txtv1CurrencyName_TextChanged"></asp:TextBox>
                                    </td>
                                    <td style="width: 10%">
                                        <asp:Button ID="btnv1Search" runat="server" CssClass="btn-success" Text="Search"
                                            OnClick="btnv1Search_Click" TabIndex="3" />
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="4">
                                        <asp:GridView ID="gvCurrency" runat="server" CssClass="mGrid" HorizontalAlign="Center"
                                            Width="100%" AutoGenerateColumns="False" OnRowCommand="gvCurrency_RowCommand"
                                            AllowPaging="True" OnPageIndexChanging="gvCurrency_PageIndexChanging">
                                            <Columns>
                                                <asp:TemplateField HeaderText="Currency Code">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblv2CurrencyCode" runat="server" Text='<%# Bind("CurrencyCode") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Currency Name">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblv2CurrencyName" runat="server" Text='<%# Bind("CurrencyName") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField ShowHeader="False">
                                                    <ItemTemplate>
                                                        <asp:ImageButton ID="imgbtnView" runat="server" CommandArgument="<%# Container.DisplayIndex %>"
                                                            CommandName="ViewData" ImageUrl="~/Images/icon_view.png" ToolTip="View" />
                                                    </ItemTemplate>
                                                    <ItemStyle HorizontalAlign="Center" Width="3%" />
                                                </asp:TemplateField>
                                                <asp:TemplateField ShowHeader="False">
                                                    <ItemTemplate>
                                                        <asp:ImageButton ID="imgbtnEdit" runat="server" CommandArgument="<%# Container.DisplayIndex %>"
                                                            CommandName="EditData" ImageUrl="~/Images/icon_edit.png" ToolTip="Edit" />
                                                    </ItemTemplate>
                                                    <ItemStyle HorizontalAlign="Center" Width="3%" />
                                                </asp:TemplateField>
                                                <asp:TemplateField ShowHeader="False">
                                                    <ItemTemplate>
                                                        <asp:ImageButton ID="imgbtnDelete" runat="server" CommandArgument="<%# Container.DisplayIndex %>"
                                                            OnClientClick="return ConfirmOnDelete();" CommandName="DeleteData" ImageUrl="~/Images/icon_delete.png"
                                                            ToolTip="Delete" />
                                                    </ItemTemplate>
                                                    <ItemStyle HorizontalAlign="Center" Width="3%" />
                                                </asp:TemplateField>
                                            </Columns>
                                            <PagerStyle CssClass="pgr" />
                                            <AlternatingRowStyle CssClass="alt" />
                                        </asp:GridView>
                                        <div class="text-right badge">
                                            <asp:Label ID="lblCount" runat="server"></asp:Label>
                                        </div>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </asp:View>
                <asp:View ID="View2" runat="server">
                    <div class="box">
                        <div class="head">
                            <h1>
                                Currency</h1>
                        </div>
                        <div class="inner">
                            <table style="width: 100%">
                                <tr>
                                    <td class="tdleftcolumn">
                                        <span class="textfield">Currency Code:</span>
                                    </td>
                                    <td class="tdrightcolumn">
                                        <asp:TextBox ID="txtv2CurrencyCode" runat="server" CssClass="control-textbox validate[required,custom[onlyLetterSp]]"
                                            MaxLength="3" TabIndex="1" Width="20%" AutoPostBack="True" OnTextChanged="txtv2CurrencyCode_TextChanged"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="tdleftcolumn">
                                        <span class="textfield">Currency Name:</span>
                                    </td>
                                    <td class="tdrightcolumn">
                                        <asp:TextBox ID="txtv2CurrencyName" runat="server" Width="50%" CssClass="control-textbox validate[required,custom[onlyLetterSp]]"
                                            MaxLength="50" TabIndex="2"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="tdleftcolumn">
                                        &nbsp;
                                    </td>
                                    <td class="tdrightcolumn">
                                        &nbsp;
                                    </td>
                                </tr>
                                <tr>
                                    <td class="tdleftcolumn">
                                        &nbsp;
                                    </td>
                                    <td class="tdrightcolumn">
                                        <asp:Button ID="btnv2Add" runat="server" CssClass="btn-success" OnClick="btnv2Add_Click"
                                            TabIndex="3" Text="Add" />
                                        <asp:Button ID="btnv2Confirm" runat="server" Text="Confirm" CssClass="btn-success"
                                            OnClick="btnv2Confirm_Click" TabIndex="4" />
                                        <asp:Button ID="btnv2Save" runat="server" CssClass="btn-success" OnClick="btnv2Save_Click"
                                            TabIndex="5" Text="Save" />
                                        <asp:Button ID="btnv2Edit" runat="server" CssClass="btn-primary" OnClick="btnv2Edit_Click"
                                            TabIndex="6" Text="Edit" />
                                        <asp:Button ID="btnv2Delete" runat="server" CssClass="btn-danger" OnClick="btnv2Delete_Click"
                                            OnClientClick="return confirm('Are you sure you want to delete?');" TabIndex="7"
                                            Text="Delete" />
                                        <asp:Button ID="btnv2Clear" runat="server" CssClass="btn-warning" OnClick="btnv2Clear_Click"
                                            TabIndex="8" Text="Clear" />
                                        <asp:Button ID="btnv2Cancel" runat="server" CssClass="btn-warning" OnClick="btnv2Cancel_Click"
                                            TabIndex="9" Text="Cancel" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="tdleftcolumn">
                                        &nbsp;
                                    </td>
                                    <td class="tdrightcolumn">
                                        <asp:Label ID="lblErrorAdd" runat="server" CssClass="control-label"></asp:Label>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </asp:View>
            </asp:MultiView>
        </ContentTemplate>
    </asp:UpdatePanel>
    <script type="text/javascript">
        Sys.Application.add_load(jScript);

        function jScript() {
            $("[id*=btnv2Add]").click(function () {
                $("#ctl01").validationEngine('attach', { promptPosition: "topRight" });
            });
            $("[id*=btnv2Save]").click(function () {
                $("#ctl01").validationEngine('attach', { promptPosition: "topRight" });
            });
            $("[id*=btnv2Clear]").click(function () {
                $("#ctl01").validationEngine('detach');
            });
            $("[id*=btnv2Cancel]").click(function () {
                $("#ctl01").validationEngine('detach');
            });
        };

        var prm = Sys.WebForms.PageRequestManager.getInstance();
        if (prm != null) {
            prm.add_initializeRequest(function (sender, e) {
                if (sender._postBackSettings.panelsToUpdate.join().indexOf("UpdatePanel1") != -1) {
                    if (!$("[id*=UpdatePanel1]").validationEngine('validate')) {
                        e.set_cancel(true);
                    }
                }
            });
        }
    </script>

当我将更新面板放入表单时,我的表单验证无效。验证引擎仅在btnv2Add点击时附加。我该如何解决这个问题?我附上了我的asp.net源代码和java脚本。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。

Sys.Application.add_load(jScript);

function jScript()
{
 $("[id*=btnv2Add]").click(function () {
                $("#ctl01").validationEngine('attach', { promptPosition: "topRight" });
            });
            $("[id*=btnv2Save]").click(function () {
                $("#ctl01").validationEngine('attach', { promptPosition: "topRight" });
            });
            $("[id*=btnv2Clear]").click(function () {
                $("#ctl01").validationEngine('detach');
            });
            $("[id*=btnv2Cancel]").click(function () {
                $("#ctl01").validationEngine('detach');
            });
        });

        var prm = Sys.WebForms.PageRequestManager.getInstance();
        if (prm != null) {
            prm.add_initializeRequest(function (sender, e) {
                if (sender._postBackSettings.panelsToUpdate.join().indexOf("UpdatePanel1") != -1) {
                    if (!$("[id*=UpdatePanel1]").validationEngine('validate')) {
                        e.set_cancel(true);
                    }
                }
            });
}