Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Templated Custom Control

Reply
Thread Tools

Templated Custom Control

 
 
Rupali Waykole
Guest
Posts: n/a
 
      10-10-2009
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(DesignerSerializat ionVisibility.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/tutorials...-studio-n.aspx
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling templated member of templated object david@sunlightd.com C++ 1 06-22-2007 05:13 PM
templated function as parameter of another templated function Amadeus W. M. C++ 2 07-04-2006 09:59 PM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57