Selective Validation of Controls (text boxes) in a Repeater control

Last post 08-22-2007 9:24 PM by Allen Chen – MSFT. 6 replies.

Sort Posts:

  • Selective Validation of Controls (text boxes) in a Repeater control

    08-17-2007, 9:54 AM
    • Loading...
    • ljenner01
    • Joined on 07-17-2007, 5:27 PM
    • Posts 61

    The objective: Display a list of prices for various admission tickets. Some tickets in the list are free (i.e. children under 3), some items are not admission tickets (e.g. venue guide, etc.).

    Business rule: Bookings cannot be made for any less than 15 paying visitors. 

     
    I have the following Repeater control which is populated by a stored procedure returning a DataSet of all the 'tickets' (see above) available for the date the user has chosen to visit the venue:

     

    1    <asp:Repeater ID="rptrTickets" runat="server" OnItemDataBound="BindOnChangeEvents">
    2 <ItemTemplate>
    3 <tr class="item" id=<%# Eval("TicketTypeID") %>>
    4 <td><%# Eval("Product") %></td>
    5 <td runat="server" id="Price"><%# Eval("Price") %></td>
    6 <td>
    7 <asp:Literal runat="server" ID="TicketType" Visible="false" Text=<%# Eval("TicketTypeID") %>></asp:Literal>
    8 <asp:TextBox ID="txtQty" MaxLength="3" style="width: 3em;" runat="server" ></asp:TextBox>
    9 <asp:RangeValidator ID="rngvTxtQty" MinimumValue="0" MaximumValue="999" ControlToValidate="txtQty" EnableClientScript="true" Type="Integer" runat="server" Text=" *"></asp:RangeValidator>
    10 <!-- Need to find a way to disallow white space
    11 <asp:RegularExpressionValidator ID="revTxtQty" ControlToValidate="txtQty" runat="server" ValidationExpression="^\d+$" Text=" *"></asp:RegularExpressionValidator>
    12 -->

    13 </td>
    14 <td>£<asp:Label ID="lblTotal" runat="server" enableviewstate="true" Text="0.00">0.00</asp:Label></td>
    15 </tr>
    16 </ItemTemplate>
    17
    18 </asp:Repeater>
    19 <asp:ValidationSummary ID="ValidationSummary1" HeaderText="Please enter only numbers in the marked (*) fields:" runat="server" />

     I am trying to figure out how i can do a server side check (client side will need to be in place as well, eventually) to enforce the business rule (as above).

    I have already found a method of looping through all the repeater items and determining the quantity of tickets per ticket type, thus allowing me to determine whether the input meets the requirement or not. However, this is occurring in response to a button click event, so the page as already been through the validation stage of the page lifecycle.

    What i would like to be able to do is, if the business requirement is not met, change the HeaderText of the ValidationSummary control (or even a new validation control, or even a label, doesn't matter) to display an error message, and then basically point the user back to the same page again with the updated error message.

     So in short, how can i perform this kind of server side validation?

    Where the result depends on the sum of the contents of an unknown number of text boxes in a repeater control and where the content of only some of the text boxes are to be included in the summation.

    Any thoughts/suggestions/advice for better approach would be most appreciated!

    Thanks in advance,

    lj.

     

  • Re: Selective Validation of Controls (text boxes) in a Repeater control

    08-22-2007, 12:33 AM
    Answer

    Hi:

    ljenner01:
    10 <!-- Need to find a way to disallow white space
    11 <asp:RegularExpressionValidator ID="revTxtQty" ControlToValidate="txtQty" runat="server" ValidationExpression="^\d+$" Text=" *"></asp:RegularExpressionValidator>
    12 -->

     

      Here you can use RequiredFieldValidator Control.

     

    ljenner01:
    So in short, how can i perform this kind of server side validation?

     

      From your description I think you'd better use CustomValidator control.

      You can gest started from here:

    http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/validation/customvalidator.aspx

      If your problem isn't solved, please inform us.

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Selective Validation of Controls (text boxes) in a Repeater control

    08-22-2007, 4:20 AM
    • Loading...
    • ljenner01
    • Joined on 07-17-2007, 5:27 PM
    • Posts 61

    Hi Allen,

     Perhaps i did not explain myself clearly enough regarding the 'disallow white space'. Each txtQty field is not a required field at all. But, if users are to enter information into that text box, they are only allowed to enter a numerical character or nothing...

    Currently, the RangeValidator ensures that only digits between 0 and 999 are entered, yet it still allows a space to entered as valid input. So if a user enters "<space>2", then an exception is thrown when i attempt to:

    Convert.ToInt32(txtQty.Text)

    In terms of the more serious issue of validating the correct number of tickets...I'll have to go away and do playing.

    In the mean time, if anyone has suggestions regarding how to disallow spaces being entered into a text box I would love to hear them!

  • Re: Selective Validation of Controls (text boxes) in a Repeater control

    08-22-2007, 5:33 AM

    Hi:

      Can this work?


            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox5"
                ErrorMessage="RegularExpressionValidator" ValidationExpression="^\d{0,3}$">

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Selective Validation of Controls (text boxes) in a Repeater control

    08-22-2007, 8:32 AM
    Answer
    • Loading...
    • ljenner01
    • Joined on 07-17-2007, 5:27 PM
    • Posts 61

    Hi Allen,

     I have investigated the use of a Custom Validation control and it definatley seems like the right option. However, I have a few issues/queries.

    Here is the updated code:

     

    1    <asp:Repeater ID="rptrTickets" runat="server" OnItemDataBound="BindOnChangeEvents">
    2                <ItemTemplate>
    3                    <tr class="item"  id=<%# Eval("TicketTypeID") %>>
    4                        <td><%# Eval("Product") %></td>
    5                        <td runat="server" id="Price">£<asp:Label ID="lblPrice" Text=<%# Eval("Price", "{0:f}") %> runat="server"></asp:Label></td>
    6                        <td>
    7                            <asp:Literal runat="server" ID="TicketType" Visible="false" Text=<%# Eval("TicketTypeID") %>></asp:Literal>
    8                            <asp:TextBox ID="txtQty" MaxLength="3" style="width: 3em;" runat="server" ></asp:TextBox>
    9                            <asp:RangeValidator ID="rngvTxtQty" MinimumValue="0" MaximumValue="999" ControlToValidate="txtQty" EnableClientScript="true" Type="Integer" runat="server" Text=" *"></asp:RangeValidator>
    10                           <asp:CustomValidator ID="cstmTxtQty" runat="server" ControlToValidate="txtQty" OnServerValidate="ValidateTicketNumbers"></asp:CustomValidator>
    11                           <!-- Need to find a way to disallow white space
    12                           <asp:RegularExpressionValidator ID="revTxtQty" ControlToValidate="txtQty" runat="server" ValidationExpression="^\d+$" Text=" *"></asp:RegularExpressionValidator>
    13                            -->
    14                       </td>
    15                       <td>£<asp:Label ID="lblTotal" runat="server" enableviewstate="true" Text="0.00"></asp:Label></td>
    16                   </tr>
    17               </ItemTemplate>
    18               
    19           </asp:Repeater>
    20           <asp:ValidationSummary ID="ValidationSummary1" HeaderText="Please enter only numbers in the marked (*) fields:" runat="server" />
    The server side validation function:
     
    1    protected void ValidateTicketNumbers(Object sender, ServerValidateEventArgs value)
    2        {
    3            
    4            ShoppingCart cart = new ShoppingCart();
    5            BuildCart(cart);
    6            int noOfPayingTickets = (cart.NoAdults + cart.NoChildren + cart.NoConcessions);
    7    
    8            if (noOfPayingTickets < 15)
    9            {
    10               value.IsValid = false;
    11               this.ValidationSummary1.HeaderText = "You can only place a group booking for 15 or more paying visitors. Please update your ticket selection.";
    12           }
    13           else
    14           {
    15               value.IsValid = true;
    16           }
    17       }
    
     

     Issues:

    1. The server validation function (ValidateTicketNumbers) gets called once for each text box that has a value in it. This makes sense as there is an instance of this validator for each item in the repeater.

    2. Once i set the value of the ValidationSummary1.Text property on server side code, it stays at that value for the during of all future postbacks, even when noOfTickets is > 15!

    I would have thought that this would have been reset upon each page load.

    Question: How can have a custom validator existing outside the repeater control that when the user clicks the AddToCart button, the OnServerValidate function (ValidateTicketNumbers) is called. If the ticket numbers are not correct, then the HeaderText of the ValidationSummary control is updated to reflect the problem but is then reset again upon the next postback. Or would it be better to just have another Label control to contain the error message???

  • Re: Selective Validation of Controls (text boxes) in a Repeater control

    08-22-2007, 8:48 AM
    Answer
    • Loading...
    • ljenner01
    • Joined on 07-17-2007, 5:27 PM
    • Posts 61

    Issue no.1 above is fixed by simply moving the CustomValidator control outside the repeater and removing the ControlToValidate property.

    I was wrong in assuming that CustomValidator's required this property to be set.

    However, still experiencing issue 2.

  • Re: Selective Validation of Controls (text boxes) in a Repeater control

    08-22-2007, 9:24 PM
    Answer
    Hi:
      I believe this can work:
    1    protected void ValidateTicketNumbers(Object sender, ServerValidateEventArgs value)
    2        {
    3            
    4            ShoppingCart cart = new ShoppingCart();
    5            BuildCart(cart);
    6            int noOfPayingTickets = (cart.NoAdults + cart.NoChildren + cart.NoConcessions);
    7    
    8            if (noOfPayingTickets < 15)
    9            {
    10               value.IsValid = false;
    11               this.ValidationSummary1.HeaderText = "You can only place a group booking for 15 or more paying visitors. Please update your ticket selection.";
    12           }
    13           else
    14           {
      this.ValidationSummary1.HeaderText="Please enter only numbers in the marked (*) fields:";
    15               value.IsValid = true;
    16           }
    17       }
    

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter