Template Server Control Issue With Child Controls Not Being Known to Page (long mostly code sample)

Last post 12-31-2007 3:32 AM by SHOVEL. 3 replies.

Sort Posts:

  • Template Server Control Issue With Child Controls Not Being Known to Page (long mostly code sample)

    05-22-2007, 2:43 PM
    • Loading...
    • jminond
    • Joined on 07-21-2003, 6:33 PM
    • New York
    • Posts 589

    Hello, I have a control, which I will place sample code below, and aspx page using the control, ill post code there too.

    Basically, My control works like a panel, and in most cases it works fine.

    However, There is a case, where in the OnInit Method of the page, I am trying to attach an event to a button inside the template. And the runtime tells me that my button is null or not an instance.

    I was hoping someone may have some thoughts.....

    Control....

    (asp.net forum kind of killed my render code.... I can send it to anyone through email if you need more info to help me out.

    basically, My panel should kinf of cuntion like an Update Panel, where the Controls in the template are known to the aspx or ascx that is hosting my control.

     

    ASPX Page Usage:

                     

    <gbmwc:gbmcontentpanel id="ClusterListPanel" runat="server" contentheight="550" panelimagesrc="/images/gbmpanel/Loading_sm.gif"

    title="The Clusters">

    <contenttemplate>

    <p class="defaulttext-small">

    Select cluster from list below to view group/access. Use edit button<img align="absMiddle"

    alt="" hspace="5" src="/images/grids/edit.gif">image to edit basic details.</p>

    <asp:button id="btnAdd" runat="server" text="Add New..." visible="<%# CanEdit %>" /><br />

    </contenttemplate>

    </gbmwc:gbmcontentpanel>

     

    aspx.cs broken init method:
    override protected void OnInit(EventArgs e)

    {

    base.OnInit(e);

    this.btnAdd.Click += new EventHandler(this.btnAdd_Click);

    }

     

    The Control

    // [ParseChildren(true)]

    // [PersistChildren(false)]

    // [ParseChildren(true)]

    public class GBMContentPanel : WebControl { private ITemplate _ContentTemplate; private Panel contentPanel; [TemplateInstance(TemplateInstance.Single)] [TemplateContainer(typeof(System.Web.UI.TemplateControl))] [PersistenceMode(PersistenceMode.InnerDefaultProperty)] public ITemplate ContentTemplate { get { return _ContentTemplate; } set { _ContentTemplate = value; } } private Unit _Height = new Unit(-1); [PersistenceMode(PersistenceMode.Attribute)] public Unit ContentHeight { get { return _Height; } set { _Height = value; } } private string _title; public string Title { get { return _title; } set { _title = value; } } private string _PanelImageSRC = string.Empty; public string PanelImageSRC { get { return _PanelImageSRC; } set { _PanelImageSRC = value; } } private bool _showExpandImage = true; public bool ShowExpandImage { get { return _showExpandImage; } set { _showExpandImage = value; } } public bool ShowTitleBar { get { return true; } } protected override void OnPreRender(EventArgs e) { if (this.ContentTemplate != null) { contentPanel = new Panel(); this.Controls.Add(contentPanel); this.ContentTemplate.InstantiateIn(this.contentPanel); } base.OnPreRender(e); } protected override void Render(HtmlTextWriter writer) { string titleImage = ""; if (!string.IsNullOrEmpty(this.PanelImageSRC)) { titleImage = string.Format("&lt;img src='{0}' />", this.PanelImageSRC); } string sHeight = sHeight = String.Format(" height=\"{0}\"", this.ContentHeight); string cid = this.ClientID; writer.WriteLine("&lt;div id=\"{0}\" class=\"transBackground\"&gt;", cid); writer.WriteLine("&lt;div id=\"{0}_Cover\" class=\"transOFF\"&gt;", cid); writer.WriteLine("&lt;table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\" bgcolor=\"#F6F6F6\" layoutType=\"Vertical\" onmouseover=\"__gbm_Flip(true, event)\" onmouseout=\"__gbm_Flip(false, event)\" onclick=\"__gbm_ClickAjaxToolbar(event)\"&gt;"); int ColCount = 0; if (this.ShowTitleBar == true) { writer.WriteLine(" <tr>"); writer.WriteLine(" <td width=\"1\"&gt;<img src=\"/images/GBMPanel/TopLeft.gif\" /></td>"); writer.WriteLine(" <td width=\"3\"&gt;<img src=\"/images/GBMPanel/TopLeftInside.gif\" /></td>"); ColCount += 2; if (this.PanelImageSRC != String.Empty) { writer.WriteLine("&lt;td background=\"/images/GBMPanel/Top.gif\" width=\"20\"&gt;<img src=\"{0}\" /></td>", this.PanelImageSRC); ColCount++; } writer.WriteLine("&lt;td background=\"/images/GBMPanel/Top.gif\" style=\"padding-left:5px;\" width=\"100%\"&gt;<span class=\"defaulttext-small\" style=\"color:#555555\"&gt;{0}</span></td>", this.Title); ColCount++; writer.WriteLine("&lt;td width=\"13\"&gt;<img onclick=\"MinMaxGBMPanel('{0}', event);\" src=\"/images/GBMPanel/Minimize.gif\" class=\"imgBtn\" title=\"Minimize this panel\" /></td>", cid); ColCount++; writer.WriteLine("&lt;td width=\"3\"&gt;<img src=\"/images/GBMPanel/TopRightInside.gif\" /></td>"); writer.WriteLine("&lt;td width=\"2\"&gt;<img src=\"/images/GBMPanel/TopRight.gif\" /></td>"); ColCount += 2; } else { writer.WriteLine("&lt;tr height=\"2\"&gt;"); writer.WriteLine("&lt;td width=\"1\"&gt;<img src=\"/images/GBMPanel/TopLeftNoTitle.gif\" /></td>"); writer.WriteLine("&lt;td background=\"/images/GBMPanel/TopNoTitle.gif\" ></td>"); writer.WriteLine("&lt;td width=\"2\"&gt;<img src=\"/images/GBMPanel/TopRightNoTitle.gif\" /></td>"); ColCount += 3; } writer.WriteLine(" </tr>"); writer.WriteLine(" <tr id=\"{0}_trContent\"&gt;", cid); writer.WriteLine(" <td background=\"/images/GBMPanel/left.gif\"&gt;</td>"); if (sHeight == String.Empty) { writer.WriteLine("&lt;td colspan=\"{0}\"&gt;<div constrainHeight=\"0\"&gt;<div>", ColCount - 2); } else { writer.WriteLine("&lt;td colspan=\"{0}\"&gt;<div id=\"{2}_divContentHolder\" style=\"height:{1};overflow:auto;border:0px;position:relative;width:inherit;margin:2px;\"&gt;<div>", ColCount - 2, this.ContentHeight, cid); } this.contentPanel.RenderControl(writer); writer.WriteLine("&lt;/div></div></td>"); writer.WriteLine(" <td background=\"/images/GBMPanel/right.gif\"&gt;</td>"); writer.WriteLine(" </tr>"); writer.WriteLine(" <tr height=\"2\"&gt;"); writer.WriteLine(" <td><img src=\"/images/GBMPanel/BottomLeft.gif\" /></td>"); writer.WriteLine(" <td background=\"/images/GBMPanel/Bottom.gif\" colspan=\"{0}\"&gt;</td>", ColCount - 2); writer.WriteLine(" <td><img src=\"/images/GBMPanel/BottomRight.gif\" /></td>"); writer.WriteLine(" </tr>"); writer.WriteLine("&lt;/table>"); writer.WriteLine("&lt;/div>"); writer.WriteLine("&lt;/div>"); // this.contentPanel.RenderControl(writer); // base.Render(writer); } }

     

    Jonathan Minond
    http://www.Jonavi.com
    http://www.jonavi.com/Default.aspx?pageID=21
    http://RainbowBeta.com
    http://community.rainbowportal.net/blogs/jonathans_rainbow_blog/default.aspx
    http://dotnetslackers.com/community/blogs/jminond/default.aspx
  • Re: Template Server Control Issue With Child Controls Not Being Known to Page (long mostly code sample)

    05-23-2007, 2:48 AM
    • Loading...
    • hypercod3r
    • Joined on 05-23-2007, 6:21 AM
    • Posts 4

    Yea i'm grappling with the same issue, and I still havn't found a solution yet... What I was able to figure out so far though is that the problem appears to stem from the custom control or it's designer not addng a cooresponding variable to the parent hosting pages designer code behind file.

     ~H

  • Re: Template Server Control Issue With Child Controls Not Being Known to Page (long mostly code sample)

    05-23-2007, 8:12 AM
    Answer
    • Loading...
    • jminond
    • Joined on 07-21-2003, 6:33 PM
    • New York
    • Posts 589

    Well, I figured it out. The key is understanding the life cycle:
    Controls in the Template are added to the Panel Control, which in turn is added to the container control. Finally the container is added to the CustomControl. 

    1) The controls can not be known to the page. I thought I could accomplish something like what the update panel does, but I dont think I can using basic server control stuff.
    So basically, like with most controls, all the child controls should be found similar to how they are with gridviews. MyControl.FindControl("xxxxx") with a case.

    2) My TemplateContainer was acting up because of the type. I was able to obtain proper functionlaity in the end by changing my class definition likewise:
    (I think the main fix was chaning templatcontainer to be my class [TemplateContainer(typeof(GBMContentPanel))] ).

    public class GBMContentPanel : CompositeControl
    {
    .....
    [TemplateInstance(TemplateInstance.Single)]
    [
    TemplateContainer(typeof(GBMContentPanel))]
    [
    PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    [
    Browsable(false)]
    public ITemplate ContentTemplate
    {
    get
    {
    return _ContentTemplate;
    }
    set
    {
    _ContentTemplate =
    value;
    }
    }

    ....
    }

    3) Design time support was actually a lot easier and went smoothly ionce the templatecontainer was working: I made a class like :
    internal
    class GBMContentPanelDesigner : System.Web.UI.Design.TemplatedControlDesigner    which is actually kind of cool to work with, but I am still exploring all the options.

    Jonathan Minond
    http://www.Jonavi.com
    http://www.jonavi.com/Default.aspx?pageID=21
    http://RainbowBeta.com
    http://community.rainbowportal.net/blogs/jonathans_rainbow_blog/default.aspx
    http://dotnetslackers.com/community/blogs/jminond/default.aspx
  • Re: Template Server Control Issue With Child Controls Not Being Known to Page (long mostly code sample)

    12-31-2007, 3:32 AM
    • Loading...
    • SHOVEL
    • Joined on 12-12-2007, 8:28 PM
    • Posts 20

    Awsome - 60 hours of banging myhead & this was the answer...

    <TemplateInstance(TemplateInstance.[Single])> _

    <TemplateContainer(
    GetType(DualPanel))> _

    <PersistenceMode(PersistenceMode.InnerDefaultProperty)> _

    <Browsable(False)> _

    now my templates lets child objects events work.. brilliant...

     

     

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter