2011-01-19 123 views
0

我有一個網頁下面的標記現場驗證火災不合

<%@ 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

我希望進行驗證只有當「添加展覽按鈕」爲cli時纔在頁面上顯示cked。所以在我的代碼背後,我用一個「if(page.isvalid)」來檢查單擊按鈕時的頁面驗證。然而,我點擊的任何其他按鈕也會觸發驗證以及...我認爲它是因爲每次點擊按鈕都會嘗試加載一個頁面,並將驗證程序調用爲操作。我如何讓驗證工作,只有「添加展覽按鈕」觸發頁面驗證?

回答

1

您需要設置按鈕和驗證器上的ValidationGroup屬性。這樣你可以控制什麼驗證發生。

ValidationGroup屬性是一個任意標記,您可以將其添加到您的驗證器和按鈕以將它們分組爲邏輯單元。所以在你的情況下,我會這樣做:

對於與添加展覽行動相關的驗證器。

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

對於觸發您添加展覽的按鈕。

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

@And ...謝謝..我去實現你指出,並希望找到一個下拉菜單選擇驗證組沒有按預期結果。我如何指定驗證組?是否必須輸入驗證摘要的名稱,或者我必須添加某種控件來對驗證項目進行分組? – Selase 2011-01-19 08:24:38