Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing template property in render method of web control

Status
Not open for further replies.

jgd1234567

Programmer
May 2, 2007
68
GB
Hi, i'm trying to add no data template to the repeater control. I am following the article at:


Basically i add a property to the repeater:

[Browsable(false)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate EmptyDataTemplate
{
get { return _emptyDataTemplate; }
set { _emptyDataTemplate = value; }
}

The article suggests to override the OnDataBinding event with:

protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);

if (this.Items.Count == 0 && this.EmptyDataTemplate != null)
{
this.EmptyDataTemplate.InstantiateIn(this);
}
}

but this still renders the header and footer and i believe since it executed on the OnDataBinding event it won't be carried out if a postback is made.

Therefore i decided to move it to the render event:

protected override void Render(HtmlTextWriter writer)
{
if (this.Items.Count == 0 && this.EmptyDataTemplate != null)
this.EmptyDataTemplate.InstantiateIn(this);
else
base.Render(writer);
}

However it doesn't write the template to the page. If i simply put:

protected override void Render(HtmlTextWriter writer)
{
if (this.Items.Count == 0 && this.EmptyDataTemplate != null)
writer.Write("No data");
else
base.Render(writer);
}

The string no data is displayed correctly when there is no data but obviously this hasn't taken advantage of my template.

Appreciate if someone could tell me how i could write my template property in the Render method. Sorry if this is really easy but i'm new to template properties and i can't find any help on this.

Thanks
 
reading through the comments on the post
One more change. The above way won't allow you to put Asp-tags in the template (you'll get that "The Controls collection cannot be modified because the control contains code blocks..." exception).

Change the NoDataTemplate to be of type PlaceHolder, then change the method to look like this:

protected override void OnItemCreated(RepeaterItemEventArgs ea)
{
if (ea.Item.ItemType == ListItemType.Footer &&
this.Items.Count == 0)
{
Controls.Add(NoDataTemplate);
}
base.OnItemCreated(ea);
}
so instead of
Code:
[Browsable(false)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public [COLOR=red]ITemplate[/color] EmptyDataTemplate
{
    get { return _emptyDataTemplate; }
    set { _emptyDataTemplate = value; }
}
you have this
Code:
[Browsable(false)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public [COLOR=blue]PlaceHolder[/color] EmptyDataTemplate
{
    get { return _emptyDataTemplate; }
    set { _emptyDataTemplate = value; }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi, but that doesn't explain how i can write the template to the text stream.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top