Page.Loadcontrol does not load the controls

Asked

Viewed 8 times

0

I am using the Page.Loadcontrol() method to initialize an instance of a control by sending a parameter in the constructor. The instance is created, but the controls present in the file . ascx are ALL NULL as if they had not been initialized. Should I invoke some method in the constructor? How could I solve it? Thank you.

Test webpage:

public partial class Testing : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataItem dataItem = new DataItem("Jaime Palilo");
            CustomControl control = (CustomControl)Page.LoadControl(typeof(CustomControl), new object[] { dataItem });
            control.Custom_DataBinding();
            Page.Controls.Add(control);
        }
    }

public class DataItem {
    public string Name { get; set; }
    public DataItem(string name)
    {
        this.Name = name;
    }
}

Customcontrol.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CustomControl.ascx.cs" Inherits="DynamicUserControl.CustomControl" %>
<asp:Label ID="lblName" runat="server" Text=""></asp:Label>

Customcontrol.ascx.Cs

public partial class CustomControl : System.Web.UI.UserControl
    {
        DataItem DataItem;
         public CustomControl (DataItem dataItem)
        {
            this.DataItem = dataItem;
        }
        public void Custom_DataBinding()
        {
            lblName.Text = this.DataItem.Name;
        }
    }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.