halp halp - 1 year ago 115
ASP.NET (C#) Question

Find control on aspx from ascx

I'm trying to find a label on an aspx page from a user control (ascx) on said aspx page. Obviously Page.FindControl("lablel1") is not working. Do I need to add in ClientID somewhere? Thanks.

Answer Source

I think you ought to stop and think about your design. Your controls should not ever need to know anything about the page that contains them - the fact that you need to go find a control on the page from within another control tells me that you ought to rethink the problem.

The best thing I can tell you (with what little I know of your architecture) is that you ought to pass in a reference to the control you hope to find into your user control. This way your control does not have to know about things outside itself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download