?
Solved

Custom Validator inside a Repeater

Posted on 2009-07-06
1
Medium Priority
?
1,531 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 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