冲突的OnClientClick事件和RequiredFieldValidator

时间:2014-01-07 22:33:02

标签: asp.net webforms detailsview requiredfieldvalidator

在下面的代码中,我在插入和更新事件中使用了requiredfieldvalidator以及onclientclick confirm事件。

如果不满足requiredfieldvalidator,验证器会正确阻止更新或插入的发生。但是,由于按钮仍然可以点击,onclientclick确认事件仍然会触发。有没有办法阻止onclientclick确认事件被触发,直到满足requiredfieldvalidator或禁用插入/更新链接,直到满足requiredfieldvalidator为止?

我更喜欢非javascript / jquery解决方案,但如果没有别的办法,我会对它持开放态度。

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
                DataSourceID="ObjectDataSource2" CssClass="detail" 
                    OnItemInserted="UpdateGrid" CaptionAlign="Left">
                <Fields>
                    <asp:TemplateField HeaderText="APP" SortExpression="APP">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("APP") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("APP") %>'></asp:TextBox>
                            &nbsp;
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                                ControlToValidate="TextBox1" 
                                ErrorMessage="New applications must have an App_Tag"></asp:RequiredFieldValidator>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("APP") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>


                    <asp:TemplateField ShowHeader="False">
                        <EditItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                                CommandName="Update" Visible='<%# ShowEditButton() %>' OnClientClick="if (!confirm('Are you sure you want update?')) return false;" Text="Update"></asp:LinkButton>
                            <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                                CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:LinkButton ID="LinkButton9" runat="server" CausesValidation="False" 
                                CommandName="Insert" ValidationGroup="group1" Visible='<%# ShowEditButton() %>' OnClientClick="if (!confirm('Are you sure you want insert?')) return false;" Text="Insert"></asp:LinkButton>
                             <asp:LinkButton ID="LinkButton10" runat="server" 
                                CommandName="Cancel" OnClick="HidePopUp" Text="Cancel"></asp:LinkButton>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False" 
                                CommandName="Edit" OnClick="EditMode" Text="Edit" Visible='<%# ShowEditButton() %>' ></asp:LinkButton>
                            <asp:LinkButton ID="LinkButton10" runat="server" CausesValidation="False" 
                                CommandName="Cancel" OnClick="HidePopUp" Text="Close"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Fields>
            </asp:DetailsView> 

1 个答案:

答案 0 :(得分:0)

您可能会完全抛弃RequiredFieldValidator并在OnClientClick中处理验证和确认对话框(在OnClientClick中调用JavaScript函数而不是内联处理它)。我知道这是一个JavaScript解决方案,但它可能是最简单的方法。