locked
Accessing Templated Control RRS feed

  • Question

  • User2054701751 posted

    Hello,

    i need several Windowlike Panels in my WebApplication that contain other controls. So I am using a Templated Control.

     

    1    Panel.ascx.cs
    2     [ParseChildren(true)]
    3     public class Panel : UserControl, INamingContainer
    4        {
    5            protected ITemplate itbody;
    6      
    7      
    8      [PersistenceMode(PersistenceMode.InnerProperty),
    9            TemplateInstance(TemplateInstance.Single)]
    10           public ITemplate PanelContent
    11           {
    12               get { return itbody; }
    13               set { itbody = value;  }
    14           }
    15           
    16       /// <summary>
    17           /// Public interface to ensuring child controls.
    18           /// </summary>
    19           public void LoadChildControls()
    20           {
    21               
    22               // ensure the bloody bastards
    23               this.EnsureChildControls();
    24           }
    25           
    26             /// <summary>
    27           /// Insert the body the user provided in our body placeholder.
    28           /// </summary>
    29           protected override void CreateChildControls()
    30           {
    31               // insert the main content body if available
    32               if (null != PanelContent)
    33                   PanelContent.InstantiateIn(phPanelContentContainer);
    34           }
    35   }
    36   
    37  
    Foo.aspx.cs
     
    1    
    2    <%@ Register TagPrefix="cms" TagName="table" Src="~/panels/Panel.ascx" %>
    3    <cms:panel id="pnlNew" runat="server" Visible="True">
    4      <PanelContent>
    5      <asp:TextBox id="txt1" runat="server"></asp:TextBox>
    6      </PanelContent>
    7     </cms:panel>
    
     

      
    In this example the Textbox gets surrounded by some HTML. This works fine.

    But the problem is following:
    When the Panel is invisible (i.e.: after a postback) i cannot access the Textbox txt1. Then always an "Object is not referenced" Exception is thrown.
    The Textbox is NULL.

    How can i instantiate the Textbox when the Panel is invisible?

    Greetings and Thanks
    Klaus

    Monday, June 2, 2008 12:55 PM

All replies

  • User2054701751 posted

    The problem is not only occurring when the PanelControl is invisible.  The Panel itself is also not referenced.

    The Panel is NULL when i try to access pnlNew in the Page_Load of Foo.aspx.cs.

     When does .NET intialize the control?

    Monday, June 2, 2008 2:11 PM
  • User1753744165 posted

    You cant. When you set a control to Visible=False, the control isnt rendered to the page, so you cant find it. If you need to be able to find it, you'll need to set the visibility via html (display:hidden).

     

    Nick

    Monday, June 2, 2008 2:12 PM
  • User2054701751 posted

    You cant. When you set a control to Visible=False, the control isnt rendered to the page, so you cant find it. If you need to be able to find it, you'll need to set the visibility via html (display:hidden).

     

    Nick

     And how is it possible to set it to Visible = true when it is NULL after setting Visible = false? With normal asp:controls this is no problem.

     But anyway i even cant't access pnlNew in the Page_Load of the ASPX wheter its visible=true or not.

    Monday, June 2, 2008 2:28 PM
  • User1753744165 posted

    I agree, you should be able to set the panel, but I was replying to the textbox since thats what I seen and we both updated the post at the same time.

    Anyway, do you get intellisense for your control? Can you post all of your HTML source file?

     

    Nick

    Monday, June 2, 2008 3:10 PM
  • User2054701751 posted

    No i have no intellisense. 

     In my real example the Panel pnlNew has also been in another Panel. thats why it was not accessible in the Page Load. So i had the same effect like the following example.

    According to the example in the first post.  When i access the Page for the first time i get an NULL-Exception.

     
    1    public partial class test1 : System.Web.UI.Page
    2    {
    3            protected void Page_Load(object sender, EventArgs e)
    4            {
    5                txt1.Text = "Hello World";
    6            }
    7    }
    
     

    In this example the Text of the Textbox is only set after i made another postback. But this is too late for me. I want to set the textbox when i come on the page the first time.

    1     public partial class test1 : System.Web.UI.Page
    2    {
    3            protected void Page_Load(object sender, EventArgs e)
    4            {
    5                if(txt1 != null)
    6                    txt1.Text = "Hello World";
    7            }
    8    }
    

     The problem is the Controls in the Panel are initialized too late.

    Klaus

     

     

    Tuesday, June 3, 2008 3:55 AM
  • User2054701751 posted

    Now it works. 

    Here comes the NULL-Exception, because txt1 is inside the Panel pnlNew and not yet initalized.

    1    public partial class test1 : System.Web.UI.Page
    2    {
    3            protected void Page_Load(object sender, EventArgs e)
    4            {
    5                txt1.Text = "Hello World";
    6            }
    7    }

    To avoid that you have to insert pnlNew.DataBind(); in Line 5 before setting txt1.

    Then the Panel will be inserted in the ControlTree early enough and you can normally access the Textbox.

    When you have nested Panels you have to call DataBind of every Panel (starting with the most outsided panel).

    Greetings

    Klaus

    Tuesday, June 3, 2008 6:11 AM
  • User2054701751 posted

    While this works fine with a Textbox there is a problem with repeaters.

    When i call DataBind() of the Panel the DataBind() of the Repeater in the panel is also called and the repeater looses the data after a postback.

    Any idea how i can fix this?

    Klaus

    Tuesday, June 3, 2008 8:57 AM