Solved

Custom Validator inside a Repeater

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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

718 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