简体   繁体   中英

Trying to create Custom validator for 6 fields Visual Studio 2010 using C#

I am new to C# and asp.net and attempting to create a Custom-validator to ensure that one field is completed on each row.This is the code i have so far..

I would be grateful if you could advise how this is done.

<asp:Table ID="ReturnTable" runat="server" Width="100%">
        <asp:TableRow ID="ReturnDateRow" runat="server">
            <asp:TableCell ID="ReturnDateLabelCell" runat="server" Width="40%">
                <asp:Label ID="ReturnDateLabel" runat="server" Text="Employee's anticipated date of return" />
            </asp:TableCell><asp:TableCell ID="ReturnDateCell" runat="server" Width="20%">
                <asp:TextBox ID="ReturnDateTextBox" runat="server" Enabled="False" />
            </asp:TableCell><asp:TableCell ID="ReturnUnknownCell" runat="server" Width="35%">
                <asp:CheckBox ID="ReturnUnknownCheckBox" runat="server" Text="Not known" Checked="True" AutoPostBack="True" />
            </asp:TableCell><asp:TableCell ID="ReturnDatePadCell" runat="server" Width="5%">
                &nbsp;
            </asp:TableCell></asp:TableRow><asp:TableRow ID="FirstDateRow" runat="server">
            <asp:TableCell ID="FirstDateLabelCell" runat="server" Width="40%">
                <asp:Label ID="FirstDateLabel" runat="server" Text="First day of sickness" />
            </asp:TableCell><asp:TableCell ID="FirstDateCell" runat="server" Width="20%">
                <asp:TextBox ID="FirstDateTextBox" runat="server" Enabled="true" />
            </asp:TableCell><asp:TableCell ID="FirstDateUnknownCell" runat="server" Width="35%">
                <asp:CheckBox ID="FirstDateUnknownCheckBox" runat="server" Text="Not known" Checked="True" AutoPostBack="True" />
            </asp:TableCell><asp:TableCell ID="FirstDatePadCell" runat="server" Width="5%">
                &nbsp;
            </asp:TableCell></asp:TableRow><asp:TableRow ID="ActualDateRow" runat="server">
            <asp:TableCell ID="ActualDateLabelCell" runat="server" Width="40%">
                <asp:Label ID="ActualDateLabel" runat="server" Text="Actual date of return" />
            </asp:TableCell><asp:TableCell ID="ActualDateCell" runat="server" Width="20%">
                <asp:TextBox ID="ActualDateTextBox" runat="server" Enabled="true" />
            </asp:TableCell><asp:TableCell ID="ActualDateUnknownCell" runat="server" Width="35%">
                <asp:CheckBox ID="ActualDateUnknownCheckBox" runat="server" Text="Not known" Checked="True" AutoPostBack="True" />
            </asp:TableCell><asp:TableCell ID="ActualDatePadCell" runat="server" Width="5%">
                &nbsp;
            </asp:TableCell></asp:TableRow></asp:Table>

Add a CustomValidator control to the page and set the following properties:

ControlToValidate

The ID of the control you are validating.

ErrorMessage , Text, Display

Properties that specify the text and location of the error or errors that will display if the validation fails. For details, see How to: Control Validation Error Message Display for ASP.NET Server Controls.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

if you want to validate above textbox

<asp:CustomValidator ID="CustomValidator1" runat="server" ForeColor="Red" ErrorMessage="Please enter numeric values."OnServerValidate="Validate_Numeric" ControlToValidate="TextBox1"></asp:CustomValidator>

in your case :

        <asp:TableCell ID="ReturnDateCell" runat="server" Width="20%">
            <asp:TextBox ID="ReturnDateTextBox" runat="server" Enabled="False" />
            <asp:CustomValidator ID="CustomValidator1" runat="server" ForeColor="Red" ErrorMessage="Please enter numeric values." ControlToValidate="ReturnDateTextBox"></asp:CustomValidator>
        </asp:TableCell>

I think this is what you are looking...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM