accessing controls in web forms user control

  • Thread starter Thread starter arvee
  • Start date Start date
A

arvee

Is there a way to access controls (and their properties) in a user control?
The Web Form Designer marks controls as 'Protected' which makes them
inaccessable from the host form. If I mark them as Public, I can access
them, but the next time the controls are manipulated in the design mode,
they are converted back to Protected. Is there an obvious/easy way around
this?

Thanks.
 
Hi ;)

How about exposing the controls you're interested in as public properties?

e.g.

protected System.Web.UI.WebControls.TextBox m_editName;

public TextBox Name {
get { return m_editName; }
}
 
Hi Lee,

This is where arvee is having trouble.

The VS.Net designer re-marks them as "protected" everything it is
accessed in DesignMode.

But arvee,

I think it can be made public in the "properties" pane. Right click
the control, select properties and make it "public" through that entry.
You should be able to do it.


--
Cheers,
Gaurav Vaish
http://mastergaurav.blogspot.com
http://mastergaurav.org
------------------
 
MasterGaurav,

My user control contains several web form server controls such as label
controls. My hope was to add the user control to a web form and
programatically set the .Text property of the labels. For example:

Dim newUserControl as myUserControl = LoadControl("blah.apcx")
newUserControl.lblTitle.Text = "Hello World!"

As the Web Form Designer sets the child controls on the user control as
Protected, the code on the Parent web form is restricted from accessing
them. I suppose its a good object design, but in my case, I'd like to
get easy access to them (yeah, I'm lazy).

I worked around it for now by adding my own Public properties that are
exposed from the parent form. In the Page_Load event of the user
control, I assign the public properties (string type) to the .Text
properties of the label controls.

Seemed like there may be an easier way, but hey, that's why we get paid
the big bucks (ha ha).
 
I understood that he was trying to change the declaration of the control.

e.g. trying to change
protected TextBox Name
to
public TextBox Name

What I'm proposing is adding a public property which simply delegates to
the control declaration.

I tested this prior to posting, hoping to verify that the designer won't
remark the property (it doesn't) - I also noted that it doesn't remark
the control declaration either (certainly not when changing protected to
public in the code editor - I also tried switching back and forth between
the form view, html view and code view), so I dunno what problem arvee is
having.
 
Back
Top