2

I have a custom control that I have created with a bunch standard windows asp controls on it.

Question:

Is it possible to inherit the properties of the parent control into the custom control without re-inventing the wheel?

So for example I have a control with a Button, a TextBox, and a Label.

Normally I can access the properties of that control via Lable1.Text however when these controls are places within a custom control how do I access them without encapsulating all the properties of that control individually.

I was hoping for something like CustomControl1.Lable1.Text or is this not possible

If I use this public Label lbMovieName { get { return this.lbMoveName; } set { lbMovieName = value; } }

I get what I need but can you please tell me why I should not do it?

MrBronz
  • 63
  • 2
  • 9
  • 1
    Have you tried this.Label1.Text ? – Felix Apr 05 '13 at 14:47
  • You could expose the the internal controls with public read only properties. Though it's probably better practice to just expose the properties of the internal controls that make sense for your custom control. – juharr Apr 05 '13 at 14:56
  • 1
    @tijizor I believe the OP is talking about getting at the Lable1.Text from outside of the custom control. – juharr Apr 05 '13 at 14:58
  • Juharr yes that's exactly what I want, but its not just he text property of a label its all the properties of any control I add – MrBronz Apr 05 '13 at 15:13

3 Answers3

2

The easiest way is to expose the control through a public read-only property:

public Label MyLabel
{
    get { return this.Label1; }
}

However encapsulating just the values you want to expose is definitely a cleaner solution for several reasons:

  1. you can abstract away that actual control type versus being tied to a Label in this case - if you expose the control it will be difficult to swap out the Label with MyNewCoolLabel, for example
  2. You may be exposing more that you want to - the client could change the display properties of the label, etc.
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • D Stanley I'm not sure what you point is in regards to the client being able to change the display properties, Surely if they want to display something differently they should be able to... shouldn't they – MrBronz Apr 05 '13 at 15:23
  • @My point is the client (the page that _uses_ the control) could change ANY property of the control, not just the text - the ID, font, color, size, position, CSS Class, possibly even add event handlers (I've never tried it to know). For labels it may not be a big deal but for buttons and input controls it could expose risks that you don't think of. If you _want_ to expose all of the properties of the control that's fine, but encapsulating the major properties (Text is usually the big one) is a safer and more common practice. – D Stanley Apr 05 '13 at 15:27
  • Pardon my ignorance but first point how would one do that? second point adding adding an event handler requires manipulation in the code behind. And an in-depth knowledge of the system its working on don it? – MrBronz Apr 05 '13 at 15:31
  • "how would one to that" - `CustomControl1.Label1.CssClass = "MyCssClass";` `CustomControl1.TextBox1.OnTextChanged += this.SendEmailToAfghanistan;` I don't know if adding event handlers like that would work in ASP.NET but the point is don't expose any more that you _have_ to. Make it _seem_ like it's all one control with custom _properties_. – D Stanley Apr 05 '13 at 15:49
  • A-ha now that make scene... It would be interesting to know if like you said if this is possible in .Net. still how would you introduce that into a system? would you need some special tool to inject it in??? – MrBronz Apr 05 '13 at 16:15
  • To answer your previous question, it would be done in the code-behind of the _form using the control_. A rendered page would _not_ have the ability to inject server-side code as an event handler. But a form _using_ your control could possibly attach a server-side event handler to one of the form's controls if you expose it. Again, if you encapsulate the controls you don't have to worry about it (and you get other benefits as well). Exposing the control itself is a short-cut that may leave you vulnerable. – D Stanley Apr 05 '13 at 17:56
  • Thank you all that answered it now I know why and also you have probably given me some peace of mind with a few security issues that I could have been exposing – MrBronz Apr 05 '13 at 19:11
1

If you are trying to avoid creating properties you can make the controls public (this is not sound OO development). As others have already mentioned you'd be much better served exposing the information that you'd want to share via properties.

Dwayne F
  • 63
  • 4
0

The best way and the best practice, I think, is to create properties of your custom control that expose only and exactly what you need. Everything else inside your control should remain private. Something like this:

public string LabelText {
    get { return this.Label1.Text; }
    set { this.Label1.Text = value; }
}

... and so on for the rest of the properties you need exposed. This will give you nice intellsense response in the designer as well.

Floremin
  • 3,969
  • 15
  • 20
  • Floremin Thats what I want to avoid I cant see the point of reinventing the wheel! – MrBronz Apr 05 '13 at 15:19
  • This is not reinventing the wheel. It's OO programming: you expose what needs to be exposed and hide what doesn't. Other than that you could make `Label1` property of the custom control public and you'll be able to access all of its properties. – Floremin Apr 05 '13 at 15:22
  • Ok I understand that point but... If I only use lets say a Text box that is also exposing all of its properties in exactly the same way. I mean a text box not in a custom control – MrBronz Apr 05 '13 at 15:37