locked
Newbie question, child controls are null in page_load RRS feed

  • Question

  • User555856731 posted

    Hi,

    I am trying to create a custom control in VS 2005, it contains 3 textfields that are populated from an external object provided through a method.

    The markup is simply

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PersonInfo.ascx.cs" Inherits="UserManagement.PersonInfo" %>
    <div>
        <asp:TextBox ID="txtbox_Name" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="txtbox_Title" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="txtbox_Mobile" runat="server"></asp:TextBox>
    </div>


    and the codebehind looks like this (imports excluded):


        public partial class PersonInfo : System.Web.UI.UserControl
        {
            public string Name { get { return txtbox_Name.Text; } private set { txtbox_Name.Text = value; } }
            public string Title { get { return txtbox_Title.Text; } private set { txtbox_Title.Text = value; } }
            public string Mobile { get { return txtbox_Mobile.Text; } private set { txtbox_Mobile.Text = value; } }
            protected void Page_Load(object sender, EventArgs e)
            {
            }
    
    
            public void SetValues(Person person)
            {
                Name = string.Format("{0} {1}", person.Fornavn, person.Etternavn);
                Mobile = person.Mobil;
                Title = person.Title;
            }
        }

    I've tried to set a breakpoint on both Page_Load and SetValues method, but when the execution enters these methods, the child controls: txtbox_Name, txtbox_Title and txtbox_Mobile are all null. I have also tried to invoke EnsureChildControls in the Page_Load method, without any luck.

    My question is simply put, how do I get these controls to be initialized, so I can set their values?

    Any help would be greatly appreciated.

    Monday, November 30, 2009 4:49 AM

Answers

  • User-126244515 posted

    You're creating a user control, which is somewhat different to a custom control, but looking at the code and markup, I cant' see where it should fail. Mind you, if you can show us the markup for the Web Form, on which you place the User Control, then we might see a different story. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 30, 2009 5:36 AM