How to access individual controls from a form containing the web user control containing those controls


 Below is a block of code in my .aspx form named default.aspx. In this form I load a web user control. I then add it to a place holder control.(Lines 3 and 4 below) So how do I access the individual controls in my web user control? I want to set the properties on those controls, but I am baffled as to how to access the control. For example I have a text label control named "label3". How do I access this control.
Because for example, under certain situations I want to hide that control. ( Label3.Visible = false;)
 1    if(!Page.IsPostBack)
2    {
3     edAddNewZipCodes userControl = (edAddNewZipCodes)Page.LoadControl("edAddNewZipCodes.ascx");
4     PlaceHolder1.Controls.Add(userControl);
5    }
0
AppDevForMe
8/29/2007 4:18:53 PM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 6 followers.

💬 2 Replies
👁️‍🗨️ 2628 Views



After you add the control to your placeholder try the below code
Label label = (Label)userControl.FindControl("Label3");
label.Visible = false;
HC

Haissam Abdul Malak
MCAD.NET
| Blog |
0
Haissam
8/29/2007 4:32:39 PM

Unless you have a public property or method exposed by your user control which will set the visibility of this Label control, then you're out of luck. Your best bet is to add some type of Visibility property to your user control which will set the visibility of your Label. Then, you simply need to access this property from your user control reference and set it accordingly.
if (!Page.IsPostBack)
{
        edAddNewZipCodes userControl = (edAddNewZipCodes)Page.LoadControl("edAddNewZipCodes.ascx");
        userControl.Label3Visible = false;
        PlaceHolder1.Controls.Add(userControl);
}

USER CONTROL 
public bool Label3Visible
{
        get { return Label3.Visible; }
        set { Label3.Visible = value; }
}

            
Thanks, Ed

Microsoft MVP - ASP/ASP.NET

0
ecbruck
8/29/2007 4:33:38 PM
Reply: