How do I validate controls inside a repeater?

A

Alan Silver

Hello,

I have a repeater that is being used to show products from a database.
Inside the ItemTemplate is a DropDownList, that contains the numbers
0-5. The user can use this to set the quantity of that product that they
wish to order.

I want to be able to do client-side validation on the quantities when
the order button is clicked. The problems are:

1) I don't know in advance how many products there will be. I tried
getting around this by writing a Javascript variable from the
server-side script and using that as the "for" limit, like this...

<script type="text/javascript">
nprods = <%= nProds %>;
for (i=0; i<nprods; i++) {
// stuff here
}
</script>

....but this gave an error because the <head> tag has a runat="server"
attribute, and the two don't seem to go together. I tried using <%# %>
instead of <%= %> (as suggested in many places), but it didn't help.

2) I also don't know what the dropdown controls will be named in
advance.

Anyone any ideas how to get around this? TIA
 
S

Sherif Elmetainy

Use a compare Validator as shown below. This code assumes that the maximum
quantity is a field called MaxQuantity in your repeater's datasource. Also
doesn't show how to fill the DropDownList.

<asp:Repeater runat="server" ID="MyRepeater" >
<ItemTemplate>
<asp:DropDownList runat="server" ID="Quantity" />
<asp:CompareValidator runat="server"
ControlToValidate="Quantity" Type="Integer" ValueToCompare='<%#
Eval("MaxQuantity") %>' Operator="GreaterThanEqual" Text="Your validator
message" />
</ItemTemplate>
</asp:Repeater>

Regards,
Sherif
 
A

Alan Silver

Hello,

Thanks for that, but I just realised that I didn't explain the problem
totally. I am only interested in ensuring that they order something.
They don't necessarily have to order every product, just as long as they
order at least one product overall.

So the validator has to run outside of the repeater, and has to know how
to get at the child controls inside the repeater. That's where I was
getting stuck, see the original post.

The quantities dropdown will be static, such as...

<asp:DropDownList ID="drpQuantity" runat="server">
<asp:ListItem Text="0" />
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
<asp:ListItem Text="4" />
<asp:ListItem Text="5" />
</asp:DropDownList>

....so no need to worry about that bit.

Thanks for the reply. Any further help appreciated.

Sherif Elmetainy said:
Use a compare Validator as shown below. This code assumes that the maximum
quantity is a field called MaxQuantity in your repeater's datasource. Also
doesn't show how to fill the DropDownList.

<asp:Repeater runat="server" ID="MyRepeater" >
<ItemTemplate>
<asp:DropDownList runat="server" ID="Quantity" />
<asp:CompareValidator runat="server"
ControlToValidate="Quantity" Type="Integer" ValueToCompare='<%#
Eval("MaxQuantity") %>' Operator="GreaterThanEqual" Text="Your validator
message" />
</ItemTemplate>
</asp:Repeater>

Regards,
Sherif
 
S

Sherif Elmetainy

There you go

<script type="text/javascript">
var quantityLists = new Array();
function ValidateQuantity(sender, args) {
for(i = 0; i < quantityLists.length; i++) {
if(quantityLists.selectedIndex > 0) {
args.IsValid = true; return;
}
}
args.IsValid = false;
}
</script>
<asp:Repeater runat="server" ID="MyRepeater">
<ItemTemplate>
<asp:DropDownList runat="server" ID="drpQuantity" />
<script type="text/javascript">
quantityLists.push(document.getElementById('<%#
Container.FindControl("drpQuantity").ClientID %>')); </script>
</ItemTemplate>
</asp:Repeater>
<asp:CustomValidator runat="server" ID="customValidator"
ClientValidationFunction="ValidateQuantity" Text="Validation Message" />

Alan Silver said:
Hello,

Thanks for that, but I just realised that I didn't explain the problem
totally. I am only interested in ensuring that they order something. They
don't necessarily have to order every product, just as long as they order
at least one product overall.

So the validator has to run outside of the repeater, and has to know how
to get at the child controls inside the repeater. That's where I was
getting stuck, see the original post.

The quantities dropdown will be static, such as...

<asp:DropDownList ID="drpQuantity" runat="server">
<asp:ListItem Text="0" />
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
<asp:ListItem Text="4" />
<asp:ListItem Text="5" />
</asp:DropDownList>

...so no need to worry about that bit.

Thanks for the reply. Any further help appreciated.
 
A

Alan Silver

Thanks, that's exactly what I needed!

Sherif Elmetainy said:
There you go

<script type="text/javascript">
var quantityLists = new Array();
function ValidateQuantity(sender, args) {
for(i = 0; i < quantityLists.length; i++) {
if(quantityLists.selectedIndex > 0) {
args.IsValid = true; return;
}
}
args.IsValid = false;
}
</script>
<asp:Repeater runat="server" ID="MyRepeater">
<ItemTemplate>
<asp:DropDownList runat="server" ID="drpQuantity" />
<script type="text/javascript">
quantityLists.push(document.getElementById('<%#
Container.FindControl("drpQuantity").ClientID %>')); </script>
</ItemTemplate>
</asp:Repeater>
<asp:CustomValidator runat="server" ID="customValidator"
ClientValidationFunction="ValidateQuantity" Text="Validation Message" />
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top