Templated Custom Control

R

Rupali Waykole

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top