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 6960293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:23:02+00:00 2026-05-27T15:23:02+00:00

I created several user controls – most containing a single web control (text box,

  • 0

I created several user controls – most containing a single web control (text box, drop down, radio button etc) – along with one or more validation controls. The point being to combine control and validation in a single user control.

I created a base class for these user control with some common functionality – setters for several properties of a single web control, specifically CssClass and Style to be set in the control in the ascx.
Eg a single text box with a single required field validator.

Sample code for the base class:

public WebControl ctrl {get; set;}  //allow derived class access to this

public string CssClass
{
  set { ctrl.CssClass = value; }    //allow CssClass to be set in the aspx page
}

Sample code for derived class:
(in constructor or control OnInit Event – or ?)

base.ctrl = txt;    //tell the base class which web control to apply common properties to.

public string ErrorMessage
{
    set { val.ErrorMessage = value;}    //this works !
}

Sample code for ascx:

<asp:TextBox ID="txt" Cssclass="input-text-m" maxlength="50" runat="server" />
<asp:RequiredFieldValidator ID="val" runat="server" ControlToValidate="txt" 
    ErrorMessage="">*</asp:RequiredFieldValidator>

Sample code for aspx:

<uc:TextBox ID="ForeName" Cssclass="input-text-m" maxlength="50" 
ErrorMessage="Forename" runat="server"/>

The problem I found was that I couldn’t find a way for the derived class to set the base class web control reference before the base classes property setters are called.
If I set base.ctrl in the derived class constructor – then the derived class control reference (txt) is still null at this point.
If I set base.ctrl in any of the control events – eg OnInit – then this is too late.

So far I have got around the problem by simply not using a base class, and writing the property setter code in the user control class instead, however this means duplication of code, which I was trying to avoid.

Is there a way to inform the base class of the control I want it to set the properties for in advance of them being set – or am I going about things the wrong way…

  • 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-27T15:23:03+00:00Added an answer on May 27, 2026 at 3:23 pm

    What about calling EnsureChildControls before any get/set operations and including the set operation for ctrl = txt in EnsureChildControls? This is pretty standard practice for a normal servercontrol, I would think it would work for UserControls too.

    public string CssClass {   set { EnsureChildControls(); ctrl.CssClass = value; } } 
    

    Override EnsureChildControls, leaving in the call to base, and set ctrl = txt; here after the call to base.

    More information: http://msdn.microsoft.com/en-us/library/system.web.ui.control.ensurechildcontrols.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a UserControl in WPF. This user control has several text boxes that
I've created a custom user control that has several properties. One specifies which database
I have created a web user control (MemberDetails.ascx) which is loaded dynamically on a
I hope this makes sense. I have created several WPF User Controls. The lowest
I have created a WinForms user control that is a set of five cascading
I've created the graphics for the user control as a vector graphic and imported
I have web-site (for example shops catalog) with several towns. If User select his
I have a solution with several projects most of which are code or control
I have a created a user control that is hopefully going to be used
I'm building several user controls (ASCX) for my website and several of them are

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.