开发者

Field Validator Fires undesirably

开发者 https://www.devze.com 2023-02-06 00:24 出处:网络
I have a page with the following mark up <%@ Page Title=\"\" Language=\"C#\" MasterPageFile=\"~/CaseAdmin.master\" AutoEventWireup=\"true\" CodeBehind=\"AddExhibit.aspx.cs\" Inherits=\"Prototype5.

I have a page with the following mark up

<%@ 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="backgrou开发者_运维百科nd-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>

and the following interface look

Field Validator Fires undesirably

I wish to perform validation on the page only when the "add exhibit button" is clicked. so in my code behind, i used an "if(page.isvalid)" to check for page validation when the button is clicked. However any other button i click fires the validation as well... I presume its because every button click tries to load a page and that calls the validator to action. how do i allow work around the validation such that, only the " add exhibit button" triggers the page validation?


You need to set the ValidationGroup property on the button and the validator. That way you can control what validation takes place.

The ValidationGroup property is an arbitrary tag that you can add to your validators and buttons to group them into logical units. So in your case, I would do this:

For your validators that are relevant for the add exhibit action.

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

For your button that triggers your add exhibit.

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

精彩评论

暂无评论...
验证码 换一张
取 消