![]() |
|
|
|||||||
![]() |
ASP Net - accessing controls in web forms user control |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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. arvee |
|
|
|
|
#2 |
|
Posts: n/a
|
Do you want to use the controls "from outside"?
Why do you need that? I think there may be better ways to achieve what you want to do. If you can explain you base problem, it may be better. -- Cheers, Gaurav Vaish http://mastergaurav.blogspot.com http://mastergaurav.org ------------------- MasterGaurav |
|
|
|
#3 |
|
Posts: n/a
|
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; } } "arvee" <> wrote in news > 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. > > > Lee |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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). arvee |
|
|
|
#6 |
|
Posts: n/a
|
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. "MasterGaurav" <> wrote in news: oups.com: > 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 > ------------------ > Lee |
|
|
|
#7 |
|
Posts: n/a
|
Arvee:
I would recommend not providing direct acess to any field. Use properties. I know you are using your code internally (for consumption within your code) but still, why make it attack-prone by exposing the field as public? -- Cheers, Gaurav Vaish http://mastergaurav.blogspot.com http://mastergaurav.org --------------------- MasterGaurav |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| user control 1.1 | sql | Software | 0 | 04-02-2009 09:15 AM |
| Nested forms and server-side control in ASP.NET | mredelin | Software | 0 | 12-20-2007 09:12 PM |
| Mind Control and CIA'S BOURNE IDENTITY PLOT | soleilmavis@gmail.com | DVD Video | 2 | 08-03-2007 09:54 PM |
| Ajax Atlas not working in User Control | faiq | Software | 0 | 09-16-2006 08:28 AM |
| ASP.NET: User Control Events is Not Displaying in a Property Window | BabuA | Software | 0 | 09-14-2006 05:44 PM |