views:

661

answers:

1

I have a custom validator inside a repeater

<asp:Repeater runat="server" ID="lstRepeater" >
    <ItemTemplate>
        <asp:CustomValidator ID="cvValid" runat="server" CssClass="error" EnableClientScript="False" ErrorMessage="Invalid."></asp:CustomValidator>
        <asp:TextBox runat="server" ID="tbCustomAnswer"></asp:TextBox>
    </ItemTemplate>
</asp:Repeater>

As a test, I tried the following code on an OnClick event

    foreach(RepeaterItem item in lstRepeater.Items)
    {
        CustomValidator cvValid= (CustomValidator)item.FindControl("cvValid");
        cvValid.IsValid = false;
    }

As would be assumed, the error message is not displayed on the page because I didn't databind the repeater. However, as soon as re-contruct the repeater and the datasource, I lose all the old values inside the repeater. Is there an easy way around this? I can't think of an elegant way of handling this problem.

+1  A: 

Why can't you just have the OnServerValidate function return false?

 <asp:CustomValidator ID="cvValid" runat="server" CssClass="error"EnableClientScript="False" ErrorMessage="Invalid."OnServerValidate="ServerValidation" />

void ServerValidation (object source, ServerValidateEventArgs args)
 {
    args.IsValid = false;
 }
scottschulthess
You can do this, but you still have the same problem.
Eldila
As long as you don't rebind the list on PostBack this will work.
jrummell