Templated Custom Control

Discussion in 'ASP .Net' started by Rupali Waykole, Oct 10, 2009.

  1. I am creating templated user control like this
    using System;
    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;
    using System.ComponentModel;

    /// <summary>
    /// Summary description for MyTemp
    /// </summary>
    ///
    namespace abc
    {
    public class MyTemp : WebControl, INamingContainer
    {
    private StatsData statsData = null;
    [
    Browsable(false),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
    ]
    public StatsData StatsData
    {
    get
    {
    this.EnsureChildControls();
    return statsData;
    }
    }

    private ITemplate statsTemplate = null;
    [
    Browsable(false),
    DefaultValue(null),
    Description("The statistics template."),
    TemplateContainer(typeof(StatsData)),
    PersistenceMode(PersistenceMode.InnerProperty)
    ]
    public virtual ITemplate StatsTemplate
    {
    get
    {
    return statsTemplate;
    }
    set
    {
    statsTemplate = value;
    }
    }

    [Bindable(true),
    Category("Appearance"),
    DefaultValue("Site Statistics")]
    public string Title
    {
    get
    {
    object o = ViewState["DisplayStateTitle"];
    if (o == null)
    return "Site Statistics"; // return the default value
    else
    return (string) o;
    }
    set
    {
    ViewState["DisplayStateTitle"] = value;
    }
    }

    [Bindable(true),
    Category("Appearance"),
    DefaultValue("0")]
    public int TotalPostCount
    {
    get
    {
    object o = ViewState["DisplayStateTotalPostCount"];
    if (o == null)
    return 0; // return the default value
    else
    return (int) o;
    }
    set
    {
    if (value < 0)
    throw new ArgumentException("The total number of posts cannot be less than zero.");
    else
    ViewState["DisplayStateTotalPostCount"] =
    value;
    }
    }

    [Bindable(true),
    Category("Appearance"),
    DefaultValue("0")]
    public int TotalUserCount
    {
    get
    {
    object o = ViewState["DisplayStateTotalUserCount"];
    if (o == null)
    return 0; // return the default value
    else
    return (int) o;
    }
    set
    {
    if (value < 0)
    throw new ArgumentException("The total number of users cannot be less than zero.");
    else
    ViewState["DisplayStateTotalUserCount"] =
    value;
    }
    }

    protected override void CreateChildControls()
    {
    Controls.Clear(); // clear out the control hierarchy

    // create a new StatsData instance based on the property values
    this.statsData = new StatsData(this.Title, this.TotalPostCount,
    this.TotalUserCount);

    // instantiate the StatsData in the template
    StatsTemplate.InstantiateIn(statsData);

    Controls.Add(statsData); // add the StatsData to the control hierarchy
    }

    public override void DataBind()
    {
    // Create the child controls...
    CreateChildControls();
    EnsureChildControls();
    this.ChildControlsCreated = true;
    // mark that the children have been created

    base.DataBind (); // call the DataBind method
    }


    public override ControlCollection Controls
    {
    get
    {
    this.EnsureChildControls();
    return base.Controls;
    }
    }


    }

    //container claass
    public class StatsData : WebControl, INamingContainer
    {


    // private member variables
    private string statsTitle;
    private int totalPostCount;
    private int totalUserCount;

    internal StatsData(string title, int postCount, int userCount)
    {
    this.statsTitle = title;
    totalPostCount = postCount;
    totalUserCount = userCount;
    }




    public string Title
    {
    get
    {
    return this.statsTitle;
    }
    }

    public int TotalPostCount
    {
    get
    {
    return totalPostCount;
    }
    }

    public int TotalUserCount
    {
    get
    {
    return totalUserCount;
    }
    }
    }
    }

    My aspx code is as follows

    <%@ Register TagPrefix="skm" Namespace="abc"%>

    <skm:MyTemp id="anotherTemplateExample" runat="server"
    Title="Messageboard Stats" TotalPostCount="1" TotalUserCount="2">
    <statsTemplate>
    <h2><%# Container.Title %></h2>
    <ul>
    <li>
    <b>Post Count:</b> -
    <%# Container.TotalPostCount %>
    </li>
    <li>
    <b>User Count:</b> -
    <%# Container.TotalUserCount %>
    </li>
    </ul>
    </statsTemplate>
    </skm:MyTemp>


    My code is running without error but is not giving any output. Actually <%# Container.TotalUserCount %> is returning null.
    when i try same thing by code
    it says TotalUserCount property does not exist.

    what could be the solution.
    or an simple demo on Templated Custom Control

    Awaiting for ur reply
    HAve a Nice DAy





    EggHeadCafe - Software Developer Portal of Choice
    Wise for Visual Studio .NET
    http://www.eggheadcafe.com/tutorial...39-75a1f1aae2e2/wise-for-visual-studio-n.aspx
    Rupali Waykole, Oct 10, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    391
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    451
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,226
  4. Amadeus W. M.
    Replies:
    2
    Views:
    390
    Amadeus W. M.
    Jul 4, 2006
  5. Replies:
    1
    Views:
    302
Loading...

Share This Page