Field Validator不合需要地发射

时间:2011-01-19 08:17:41

标签: c# asp.net

我有一个带有以下标记的页面

<%@ Page Title="" Language="C#" MasterPageFile="~/CaseAdmin.master" AutoEventWireup="true" CodeBehind="AddExhibit.aspx.cs" Inherits="Prototype5.AddExhibit" %>

    <h2 class="style2">
    <strong><span style="color: #FFFFFF">Add Exhibit
    Form</span></strong></h2>
<div style="width: 600px">
<table style="width: 303px" align="left">
        <tr>
            <td class="style26" style="background-color: #666666">
                <p class="style5" style="color: #000000; background-color: #666666;">
                    <strong style="background-color: #666666">Select Existing Case ID:    
                </strong>    
                </p></td>
            <td class="" style="background-color: #C0C0C0" align="left">
                <asp:DropDownList ID="DropDownListcaseid" runat="server" 
                    onselectedindexchanged="DropDownListcaseid_SelectedIndexChanged">
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="DropDownListcaseid" 
                    ErrorMessage="Please select a valid case id from the dropdown menu" 
                    ForeColor="Red">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="style4" colspan="2">
                &nbsp;</td>
        </tr>
    </table>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:CMSSQL3ConnectionString1 %>" 
        SelectCommand="SELECT [CaseID] FROM [Cases]"></asp:SqlDataSource>

</div>
<div style="width: 603px; height: 75px; ">

    <table style="height: 66px; width: 598px; top: 277px; left: 268px;" 
        align="left">
        <tr>
            <td class="bold" 
                style="color: #000000; background-color: #666666; width: 127px;">
                <strong>Exhibit Type</strong></td>
            <td class="bold" style="background-color: #666666; width: 228px;">
                <span style="color: #000000">Exhibit Image</td>
            <td class="bold" style="background-color: #666666; width: 111px;">
                <span style="color: #000000">Stored Location</span></td>
            <td class="bold" style="background-color: #666666; color: #000000;">
                Officer ID</span></td>
        </tr>
        <tr>
            <td class="style32" style="background-color: #C0C0C0; width: 127px;">
                <asp:DropDownList ID="exhibitTypeDropDownList" runat="server">
                    <asp:ListItem></asp:ListItem>
                    <asp:ListItem>Hard Disk</asp:ListItem>
                    <asp:ListItem>Pen Drive</asp:ListItem>
                    <asp:ListItem>Laptop</asp:ListItem>
                    <asp:ListItem>Palm Devce</asp:ListItem>
                    <asp:ListItem>Mobile Phone</asp:ListItem>
                    <asp:ListItem>Tablet PC</asp:ListItem>
                    <asp:ListItem>Pager</asp:ListItem>
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="exhibitTypeDropDownList" 
                    ErrorMessage="Please Enter the type of exhibit. eg. Harddisk" 
                    ForeColor="Red" ondisposed="addExhibitButton_Click">*</asp:RequiredFieldValidator>
            </td>
            <td class="style28" style="background-color: #C0C0C0; width: 228px;">
                <asp:FileUpload ID="exhibitImageFileUpload" runat="server" />
            </td>
            <td class="style20" style="background-color: #C0C0C0; width: 111px;">
                <asp:DropDownList ID="storedLocationDropDownList" runat="server">
                    <asp:ListItem></asp:ListItem>
                    <asp:ListItem>B-15/4</asp:ListItem>
                    <asp:ListItem>B-10/1</asp:ListItem>
                    <asp:ListItem>B-5/4</asp:ListItem>
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                    ControlToValidate="storedLocationDropDownList" 
                    ErrorMessage="Please enter a valid stored location" ForeColor="Red" 
                    ondisposed="addExhibitButton_Click">*</asp:RequiredFieldValidator>
            </td>
            <td class="style30" style="background-color: #C0C0C0">
                <asp:DropDownList ID="DropDownList1" runat="server" 
                    DataSourceID="officersSqlDataSource" DataTextField="PoliceID" 
                    DataValueField="PoliceID" Width="79px" Height="26px">
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                    ControlToValidate="DropDownList1" 
                    ErrorMessage="Please select a valid Officer id from the dropdown list" 
                    ForeColor="Red" ondisposed="addExhibitButton_Click">*</asp:RequiredFieldValidator>
            </td>
        </tr>
    </table>

</div>
<div style="width: 609px; height: 23px;">
                </div>
<div style="margin-top:12px; width: 232px; text-align:left; font-size:1.3em;">

    <asp:SqlDataSource ID="officersSqlDataSource" 
        runat="server" 
        ConnectionString="<%$ ConnectionStrings:CMSSQL3ConnectionString1 %>" 
        SelectCommand="SELECT PoliceID FROM PoliceOfficers"></asp:SqlDataSource>
    <table align="left">
        <tr>
            <td align="center">
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" 
                    HeaderText="The following errors occured." />
            </td>
        </tr>
    </table>

    </div>
    <div style="margin-top:12px; width: 456px;">

        <table  style="width: 450px" align="left">
            <tr>
                <td align="center" colspan="3">
                    <asp:Label ID="messageLabel" runat="server" BackColor="White" ForeColor="Red" 
                        Visible="False"></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="center" style="width: 96px">
                    <asp:Button ID="cancelButton" runat="server" Text="Cancel" 
                        onclick="cancelButton_Click" height="26px" width="101px" />
                </td>
                <td  style="width: 237px">
                    &nbsp;</td>
                <td align="center">
                    <asp:Button ID="addExhibitButton" runat="server" Text="Add Exhibit" 
                        onclick="addExhibitButton_Click" />
                </td>
            </tr>
        </table>

    </div>

和以下界面看 alt text

我希望仅在单击“添加展览按钮”时才在页面上执行验证。所以在我的代码后面,我使用“if(page.isvalid)”来检查单击按钮时的页面验证。然而,我点击的任何其他按钮也会激活验证...我认为它是因为每个按钮单击都会尝试加载页面并调用验证器来执行操作。如何允许围绕验证工作,只有“添加展示按钮”才能触发页面验证?

1 个答案:

答案 0 :(得分:1)

您需要在按钮和验证器上设置ValidationGroup属性。这样您就可以控制进行验证的内容。

ValidationGroup属性是一个任意标记,您可以将其添加到验证器和按钮以将它们分组为逻辑单元。所以在你的情况下,我会这样做:

对于与添加展示操作相关的验证器。

<asp:RequiredFieldValidator ID="RequiredFieldValidator4" ValidationGroup="AddExhibit" ...

对于触发添加展览的按钮。

<asp:Button ID="addExhibitButton" ValidationGroup="AddExhibit"