Solved

Custom Validator inside a Repeater

Posted on 2009-07-06
1
1,438 Views
Last Modified: 2013-11-07
Hej everyone,

I have a repeater on my ASP.NET page where I am using a customValidator that is valid if two controls in the same RepeaterItem have some particular values.

I am doing only server side validation But CustomValidator validateServerside event is not being truggered even if I call Page.IsValid from C# code.

Please adnice as I am stuck.

Code is as below.
ASPX Page code:
 
<ItemTemplate>
        <td>
            <b><asp:label runat="server" id="lblRequired" Text="*" ></asp:label></b>
        </td>
        <td>
            <asp:Literal ID="litVarName" runat="server"></asp:Literal>
        </td>
        <td>
            <asp:TextBox ID="txtVarValue" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:Literal ID="ltrValidationComment" runat="server"></asp:Literal>
        </td>
        <td>
            <asp:CustomValidator runat="server" id="requiredVar" ControlToValidate="txtVarValue" OnServerValidate="cusCustom_ServerValidate" errormessage="Variable variable must be given" />
        </td>
        <asp:HiddenField ID="hidRegexExpression" runat="server" /> 
        <asp:HiddenField ID="hidCampaignVariableId" runat="server" /> 
    </ItemTemplate>
 
.cs File Code ... Note this is not being valled at all
 
protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e) {
            // Cast the sender object to a CustomValidator
            CustomValidator vldCustomValidator = (CustomValidator)sender;
            // Find the RepeaterItem, this is the parent object of the CustomValidator
            RepeaterItem rptItem = (RepeaterItem)vldCustomValidator.Parent;
            // Now find the control in the RepeaterItem
            TextBox txtValue = rptItem.FindControl("txtVarValue") as TextBox;
            // something to return
 
            Label lblRequired = rptItem.FindControl("lblRequired") as Label;
            HiddenField hidRegexExpression = rptItem.FindControl("hidRegexExpression") as HiddenField;
 
            if(!String.IsNullOrEmpty(hidRegexExpression.Value)) {
                if(String.IsNullOrEmpty(txtValue.Text))
                    e.IsValid = false;
                else {
                    e.IsValid = true;
                }
            }
            else {
                e.IsValid = true;
            }
        }

Open in new window

0
Comment
Question by:hussainAbid
1 Comment
 

Accepted Solution

by:
hussainAbid earned 0 total points
ID: 24784462
The above is actually working Now. The problem was I had to set
ValidateEmptyText="true" and it was not being triggered bcz TextBox value for empty string.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question