Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - accessing controls in web forms user control

 
Thread Tools Search this Thread
Old 05-26-2005, 03:26 AM   #1
Default accessing controls in web forms user control


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
  Reply With Quote
Old 05-26-2005, 05:37 AM   #2
MasterGaurav
 
Posts: n/a
Default Re: accessing controls in web forms user control
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
  Reply With Quote
Old 05-26-2005, 05:47 AM   #3
Lee
 
Posts: n/a
Default Re: accessing controls in web forms user control

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 newstale.221$zb.210@trndny04:

> 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
  Reply With Quote
Old 05-26-2005, 11:35 AM   #4
MasterGaurav
 
Posts: n/a
Default Re: accessing controls in web forms user control
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
  Reply With Quote
Old 05-26-2005, 08:35 PM   #5
arvee
 
Posts: n/a
Default Re: accessing controls in web forms user control
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
  Reply With Quote
Old 05-29-2005, 08:49 AM   #6
Lee
 
Posts: n/a
Default Re: accessing controls in web forms user control

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
  Reply With Quote
Old 06-14-2005, 06:09 AM   #7
MasterGaurav
 
Posts: n/a
Default Re: accessing controls in web forms user control
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
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46