2

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.

4

1 回答 1

1

为什么不能只让 OnServerValidate 函数返回 false?

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

void ServerValidation (object source, ServerValidateEventArgs args)
 {
    args.IsValid = false;
 }
于 2009-04-30T19:09:58.797 回答