Toran Billups
Toran Billups

Reputation: 27407

How to get the value of a control from the child into the properties of the parent in .NET?

I have a form that has a few similar controls and the parent contains the properties, but the child actually has the html controls. How could I setup my getters/setters using the "child" controls in the parent class? (Webforms - fyi)

I found the below via search, and what I'm looking for is the inverse

Getting value of a property in Parent User control from a Child user control

Edit:

I should have tried this early on, but instead found it "fun" to explain this crazy situation. The below is what worked @ 100%

Get
  Return DirectCast(Page.FindControl("lblCASE_NUMBER"), HtmlContainerControl).InnerHtml
End Get

Upvotes: 1

Views: 222

Answers (2)

Sergio
Sergio

Reputation: 8269

Follwing up on the comments:

Ok, so if i understood correctly, you want to have the controls on the usercontrols but the code on the parent(because you don't want to duplicate code).

You have 2 ways of doing this:

1 - Place your code at the parent and create properties on each child so you can access the values of the controls.

2 - Create an abstract class to place the code you don't want to duplicate and make all your childs inherit from it. you will need to create the common objects at this class as "protected abstract ObjectName" and in you childs set them as "protected override ObjectName". This option will give the least code to write but it "forces" you to understand inheritance.

Upvotes: 0

Sergio
Sergio

Reputation: 8269

If I understood you correctly you have one usercontrol (child) inside another usercontrol(parent) and you need to access the child properties from within the parent.

If this is correct all you need to do is either create properties or methods in the child as you would in any other class. Than just use them from the parent.

Upvotes: 1

Related Questions