title: Validate against repeater items
body: <p>I have a page which has a panel with a repeater items displayed on it which comes from a database table. Here's the code to display the items:</p>
  
  <p>html:</p>
  
  <pre><code>&lt;asp:Panel ID="panelLightRefreshmentsExpanded" runat="server"&gt;
    &lt;asp:Repeater ID="rptLightRefreshments" runat="server"&gt;
      &lt;HeaderTemplate&gt;
          &lt;table cellpadding="4" cellspacing="4" border="1"&gt;
              &lt;tr&gt;
                  &lt;th&gt;&lt;asp:Label ID="lblItem" Text="Item" runat="server"&gt;&lt;/asp:Label&gt;&lt;/th&gt;
                  &lt;th&gt;&lt;asp:Label ID="lblUnitPruce" Text="Unit Price" runat="server"&gt;&lt;/asp:Label&gt;&lt;/th&gt;
                  &lt;th&gt;&lt;asp:Label ID="lblQuantity" Text="Quantity" runat="server"&gt;&lt;/asp:Label&gt;&lt;/th&gt;
                  &lt;th&gt;&lt;asp:Label ID="lblUnitPriceTotal" Text="Total" runat="server"&gt;&lt;/asp:Label&gt;&lt;/th&gt;
             &lt;/tr&gt;
      &lt;/HeaderTemplate&gt;
      &lt;ItemTemplate&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;asp:Label ID="lblItem" runat="server" Text='&lt;%# Eval("Description") %&gt;' OnClick="lnkSubmit_Click" CommandArgument='&lt;% Eval("ID").ToString() %&gt;' ViewStateMode="Enabled"&gt;&lt;/asp:Label&gt;&lt;/td&gt;
              &lt;td&gt;&lt;asp:Label ID="lblUnitPrice" runat="server" Text='&lt;%# Eval("Cost") %&gt;' ViewStateMode="Enabled"&gt;&lt;/asp:Label&gt;&lt;/td&gt;
              &lt;td&gt;&lt;asp:TextBox ID="txtQuantity" runat="server" ViewStateMode="Enabled"&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;
              &lt;asp:RangeValidator ID="RangeValidatorLightRefQuantity" runat="server" ErrorMessage='&lt;%# "Quantity must be a minimum of " + Eval("MinQuantity") %&gt;' ControlToValidate="txtQuantity"
              MaximumValue="100" MinimumValue='&lt;%# Eval("MinQuantity") %&gt;' CssClass="validator" Text="*" Type="Integer" Display="Dynamic"&gt;&lt;/asp:RangeValidator&gt;
              &lt;td&gt;&lt;asp:TextBox ID="txtUnitPriceTotal" runat="server" ViewStateMode="Enabled" CssClass="grey" ReadOnly="true"&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;
              &lt;asp:TextBox ID="txtItemID" runat="server" Visible="false" Text='&lt;%# Eval("ItemID") %&gt;'&gt;&lt;/asp:TextBox&gt;
  
          &lt;/tr&gt;
      &lt;/ItemTemplate&gt;
      &lt;FooterTemplate&gt;
          &lt;/table&gt;
      &lt;/FooterTemplate&gt;
  &lt;/asp:Repeater&gt;
  &lt;/asp:Panel&gt;
  &lt;asp:CollapsiblePanelExtender ID="panelLightRefreshmentsExpanded_CollapsiblePanelExtender" 
      runat="server" Enabled="True" TargetControlID="panelLightRefreshmentsExpanded"&gt;
  &lt;/asp:CollapsiblePanelExtender&gt;
  </code></pre>
  
  <p>code behind:</p>
  
  <pre><code>SqlCommand lightRefreshmentsCommand = new SqlCommand();
                      lightRefreshmentsCommand.Connection = itemsConnection;
                      lightRefreshmentsCommand.CommandText = "select * from table where LocationAvailable LIKE '%" + site + "%' AND ItemType = 'Light refreshments' ";
  
  SqlDataReader lightRefreshmentReader = lightRefreshmentsCommand.ExecuteReader();
                          rptLightRefreshments.DataSource = lightRefreshmentReader;
                          rptLightRefreshments.DataBind();
                          lightRefreshmentReader.Close();
  </code></pre>
  
  <p>The items are catering items and in some cases a user should only be able to select one option. So for example for light refreshments if the repeater items were as follows:  </p>
  
  <ul>
  <li>Sandwiches</li>
  <li>Soup</li>
  <li>Finger Buffet 1</li>
  <li>Finger Buffet 2</li>
  <li>Finger Buffet 3</li>
  </ul>
  
  <p>A user should only be able to select one of the finger buffet options. I was going to add validation but as far as I can see you can only validate one row (ie make sure all fields on one row have an entry). Is it possible to validate against more than one row on a list of repeater items? I can't see how you would do this. Or is there another way to go about this.</p>
  
  <p>Any help gratefully appreciated.</p>
  
  <p>This is an example of the kind of help articles I've found however they have only show how to use the custom validator to validate one row not how to validate one row against another.</p>
  
  <pre><code>&lt;asp:Repeater ID="rptItem" runat="server" OnItemDataBound="rptItem_ItemDataBound"&gt;
    &lt;HeaderTemplate&gt;
        &lt;table&gt;
    &lt;/HeaderTemplate&gt;
    &lt;ItemTemplate&gt;
        &lt;tr&gt;
            &lt;td&gt;
                &lt;asp:CheckBox ID="chkSelectItem" runat="server" /&gt;
            &lt;/td&gt;
            &lt;td&gt;
                &lt;asp:TextBox ID="txtToValidate" runat="server"&gt;&lt;/asp:TextBox&gt;
            &lt;/td&gt;
            &lt;td&gt;
                &lt;asp:CustomValidator ID="cValidation" runat="server"     ClientValidationFunction="MyClientValidation"
                    ErrorMessage="Invalid"&gt;&lt;/asp:CustomValidator&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/ItemTemplate&gt;
    &lt;FooterTemplate&gt;
        &lt;/table&gt;
    &lt;/FooterTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>protected void rptItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
  {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        CheckBox chkSelectItem = e.Item.FindControl("chkSelectItem") as CheckBox;
        TextBox txtToValidate = e.Item.FindControl("txtToValidate") as TextBox;
        CustomValidator cValidation = e.Item.FindControl("cValidation") as CustomValidator;
  
        if (chkSelectItem != null &amp;&amp; txtToValidate != null &amp;&amp; cValidation != null)
        {
            ClientScript.RegisterExpandoAttribute(cValidation.ClientID, "chkId", chkSelectItem.ClientID);
            ClientScript.RegisterExpandoAttribute(cValidation.ClientID, "txtId", txtToValidate.ClientID);
        }
    }
  }
  </code></pre>