Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 900211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:20:52+00:00 2026-05-15T15:20:52+00:00

I have a UserControl which contains a TextBox and a CustomValidator. I would like

  • 0

I have a UserControl which contains a TextBox and a CustomValidator.

I would like to set the CustomValidator.ServerValidate to a method in the page that contains the UserControl

I found this code which will allow me to dynamically set the custom validators validation function:

cusvCustom.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(MethodName);

The problem is that a string value won’t work there. It needs to be a reference to the method. Is it possible to use reflection (or some other method) to get a valid reference to the parent controls method using only the string name of it? The reason I want to use the string value of the method name is so I can place the control on the page thusly:

<uc1:TextBoxField ID="tbUserName" runat="server" CustomValidationMethod="ValidateUserName" />

I did some research and I found Type.GetMethod and MethodInfo but I can’t get them to work. Primarily because I don’t know the type of the parent control and can’t figure out how to get it.

EDIT: My code for matt-dot-net

WebUserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl.ascx.cs" Inherits="WebApplication1.WebUserControl" %>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Custom Validation Failed" OnServerValidate="CustomValidator1_ServerValidate" />
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Submit" CausesValidation="true" />

WebUsecControl.ascx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebUserControl : System.Web.UI.UserControl
    {
        public ServerValidateEventHandler Validating;

        protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
        {
            if (Validating != null)
                Validating(sender, e);
        }
    }
}

TestPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="WebApplication1.TestPage" %>
<%@ Register Src="~/WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <uc1:WebUserControl ID="WebUserControl1" runat="server" OnValidating="WebUserControl1_Validating" />

    </div>
    </form>
</body>
</html>

TestPage.aspx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class TestPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //WebUserControl1.Validating += WebUserControl1_Validating;
        }

        protected void WebUserControl1_Validating(Object sender, ServerValidateEventArgs e)
        {
            e.IsValid = false;
        }
    }
}

As you can see it’s almost an exact duplicate of your code. For whatever reason it does not work for me as I have it here. When I click on the button the page reloads and is the same. When I un-comment the one line though and click the button then I see the error message.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:20:53+00:00Added an answer on May 15, 2026 at 3:20 pm

    I think this is what you want to do:

    WebUserControl.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Custom Validation Failed"
    ControlToValidate="TextBox1" OnServerValidate="CustomValidator1_ServerValidate" />
    <asp:TextBox ID="TextBox1" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="Submit" CausesValidation="true" />
    

    And in your code behind:

    public partial class WebUserControl : System.Web.UI.UserControl
    {
         public event ServerValidateEventHandler Validating;
    
         protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
         {
             if (Validating != null)
                 Validating(sender, e);
         }
     }
    

    This will allow you to use the control on a page the way you want to:

    <uc1:WebUserControl ID="WebUserControl1" runat="server" OnValidating="WebUserControl1_Validating" />
    

    Then in your .aspx.cs code behind:

    protected void WebUserControl1_Validating(Object sender, ServerValidateEventArgs e)
    {
       //do validation here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have UserControl which contains a TextBox and a Button control. The Button opens
I have a UserControl which contains a TextBox . When my main window loads
Hi. I have a UserControl which contains a textbox. I wanted to access the
I have a class which implements UserControl. In .NET 2005, a Dispose method is
I have a user control which contains a textbox. I have a class called
I have an ASP.Net web user control that contains a TextBox and a calendar
I have a complex WPF UserControl made of other ContentControl templates which contain sets
I have a base UserControl (BaseControl.cs) which has an OK and Cancel buttons on
I have user control named DateTimeUC which has two textboxes on its markup: <asp:TextBox
I have a usercontrol that has several public properties. These properties automatically show up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.