Solved

Custom Validator inside a Repeater

Posted on 2009-07-06
1
1,446 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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