NullReferenceException when user control refers to usercontrol in parent user control

I have a user control (uc1) that inherits from uc2. uc2 has a user control (uc3) declared in the markup. I am trying to access uc3 from uc1, but I am getting a NullReferenceException. I thought that due to inheritance, uc3 will create an instance, but it seems like I'm missing a step.

Clarification:

How does the controlling child user inherit markup from the base class? The server controls in the base user control are null in the code behind the base user control. Why?

+3
source share
4

FindControl . Rick Strahl: ASP.NET 2.0 MasterPages FindControl() . , .

.Net 1.1 2.0, 1.1 aspx, , 2.0 , , . http://msdn.microsoft.com/en-us/library/ms227671.aspx

0

uc1.uc3 = new uc3, uc1? .

0

Uc2: UserControl
{        Uc3 _uc3;        Uc3 Uc3       {           get {return _uc3; }       } }

Uc3: UserControl { }    Uc1: Uc2   {       public Uc1()       {           Uc3 uc3 = this.Uc3;           //           Uc3 uc3_interface = ((IUc3) ).Uc3;       }   }    IUc3   {       Uc3 Uc3 {get; }   }   public class PageContainer: , IUc3   {        Uc2 _uc2;// , ,

    public Uc3 Uc3
    {
        get { return _uc2.Uc3; }
    }
}
0
source

Finding a control can do the job, but it's not a good practice. If you ever change the identifier of a control in the markup, the compiler will not catch it, avoid using the system at all costs; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;

WebApplication5 namespace {

public class Uc2: UserControl  
{
    private Uc3 _uc3;

    public Uc3 Uc3
    {
        get { return _uc3; }
    }
}
public class Uc3 : UserControl
{
}
public class Uc1 : Uc2
{
    public Uc1()
    {
        Uc3 uc3 = this.Uc3;
        //or
        Uc3 uc3_interface = ((IUc3)Page).Uc3;
    }
}
public interface IUc3
{
    Uc3 Uc3 { get; }
}
public class PageContainer : Page, IUc3
{
    private Uc2 _uc2; //must be assigned or must exist in markup thus designer

    public Uc3 Uc3
    {
        get { return _uc2.Uc3; }
    }
}

}

0
source

Source: https://habr.com/ru/post/1747059/


All Articles