0

代码摘录:

<asp:UpdatePanel runat="server" ChildrenAsTriggers="true">
<ContentTemplate>

    <asp:ValidationSummary runat="server" DisplayMode="List" />

    <asp:GridView ID="MyGridView" runat="server" AllowPaging="true" AllowSorting="true"
            DataKeyNames="itemId"
            OnRowDataBound="MyGridView_RowDataBound"
            onPageIndexChanging="MyGridView_PageIndexChanging"
            onSorting="MyGridView_Sorting"
            OnRowEditing="MyGridView_RowEditing"
            OnRowCancelingEdit="MyGridView_RowCancelingEdit"
            OnRowUpdating="MyGridView_RowUpdating"
            OnRowDeleting="MyGridView_RowDeleting"
            OnRowCommand="MyGridView_RowCommand"
            AutoGenerateColumns="false"
            onSelectedIndexChanged="MyGridView_SelectedIndexChanged"
            CssClass="adminTable">

            <Columns>
               <asp:TemplateField HeaderText="Name" SortExpression="name">
                    <EditItemTemplate>
                        <asp:TextBox ID="editName" runat="server" Text='<%# Bind("name") %>' />
                        <asp:RequiredFieldValidator runat="server" ControlToValidate="editName"
                            ErrorMessage="Please enter a Name" Display="None" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="name" runat="server" Text='<%# Bind("name") %>' />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="newName" runat="server" />
                        <asp:RequiredFieldValidator runat="server" ControlToValidate="newName"
                            ErrorMessage="Please enter a Name" Display="None" />
                    </FooterTemplate>
                </asp:TemplateField>

                <%-- Some additional Similar Templates here --%>

                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:LinkButton ID="linkUpdate" runat="server" CausesValidation="true"
                            CommandName="Update" Text="Save" />
                        <asp:LinkButton ID="linkCancel" runat="server" CausesValidation="false"
                            CommandName="Cancel" Text="Cancel" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="linkEdit" runat="server" CausesValidation="false"
                            CommandName="Edit" Text="Edit" />
                        <asp:LinkButton ID="linkDelete" runat="server" CausesValidation="false"
                            CommandName="Delete" Text="Delete"
                            OnClientClick="return confirmDelete();" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="linkUpdate" runat="server" CausesValidation="true"
                            CommandName="Insert" Text="Save" />
                        <asp:LinkButton ID="linkCancel" runat="server" CausesValidation="false"
                            CommandName="Cancel" Text="Cancel" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <pagersettings mode="Numeric" position="Top" pagebuttoncount="10" />
            <pagerstyle height="30px" verticalalign="Top" horizontalalign="Right" />
    </asp:GridView>

    <asp:LinkButton ID="LinkAdd" runat="server" OnClick="LinkAdd_OnClick" Text="Add"
        CausesValidation="false" />

</ContentTemplate>
 </asp:UpdatePanel>

设想:

单击任意行中的编辑按钮。代码隐藏 OnRowEditing 集MyGridView.EditIndex = e.NewEditIndex;和其他几个小任务。将名称字段编辑为空白。单击以保存在该行中。相应的消息与验证摘要一起显示在顶部。现在单击该行中的取消。没发生什么事。单击任意位置的任何其他按钮(排序、分页、在不同行中编辑等)。没发生什么事。

只要所有字段保持有效,一切都会有效。此外,在无效提交后,可以输入编辑字段,但这不会改变结果。

各种事件处理程序(即 OnRowCancelingEdit、OnRowCommand、OnRowDeleting 等)中的断点表明没有调用进入代码隐藏。

当 GridView 不在 UpdatePanel 中时,一切都按预期工作。但是,希望在 UpdatePanel 中有这些,这样页面就不会“闪烁”或每个页面、排序或编辑。

尝试的事情:

  • 将 UpdatePanel 的 UpdateMode 更改为 Always 和 Conditional。
  • 删除 ChildrenAsTriggers
4

1 回答 1

0

我从 SO 以外的人那里得到了这个答案,我觉得这是我自己的答案很尴尬。但如果它对其他人有帮助,我觉得我应该在这里展示它。

我必须将所有验证器和摘要放入ValidationGroups 并将所有链接和按钮设置为CausesValidation="false". 然后,在 Code-Behind 中,在相应方法的顶部,如果失败Page.Validate("groupName"),则立即调用。return;这会强制进行重新设置的回发UpdatePanel

于 2013-01-30T20:19:39.093 回答