SiteMapPath nodeTemplate custom definition

本文探讨了如何在代码中手动创建 SiteMapPath 控件的 NodeTemplate,并通过实现 ITemplate 接口来定制节点的显示方式。作者提供了一个简化示例,展示了如何为每个节点动态地设置标题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SiteMapPath nodeTemplate custom definition
p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;font-size:10.5pt;font-family:'Times New Roman';} p {margin-right:0cm;margin-left:0cm;font-size:7.5pt;font-family:Verdana;} span.EmailStyle17 {font-family:Arial;color:windowtext;} @page Section1 {size:595.3pt 841.9pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;layout-grid:15.6pt;} div.Section1 {page:Section1;}

Reference: http://www.jordomedia.com/RSS/l_op=viewrss/lid=36720.html

This is a bit of a follow on to my last post. At the end, I made an update about how to make use of custom TreeNodes in a databound scenario. I did that example with the CSSTreeNode instead of the TemplatedTreeNode because the custom property in the templated case was an ITemplate. Trying to assign a value to that property is a great deal more difficult than a string property. And that got me thinking...

Lets start with something that seems pretty simple:

<asp:SiteMapPath ID="SiteMapPath1" runat="server">
<
NodeTemplate>
<%# Eval("title") %>
</NodeTemplate>
</
asp:SiteMapPath>

What if you wanted to add this control to the page dynamically? How would you write it in code? Well, adding the SiteMapPath to the page is pretty simple. But the NodeTemplate isn't so simple. What actually happens in that case is a mechanism in the framework creates a new object on the fly that implements the ITemplate interface and generates some code to populate the template. In order to do this manually, the developer would have to create his/her own ITemplate object and databinding code. Here's a simplified version of what that code might look like:

public class MyTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
Label l1 = new Label();
container.Controls.Add(l1);
l1.DataBinding +=
new EventHandler(l1_DataBinding);
}

void l1_DataBinding(object sender, EventArgs e)
{
Label l1 = sender as Label;
SiteMapNodeItem container = l1.BindingContainer as SiteMapNodeItem;
l1.Text = container.SiteMapNode.Title;
}
}

public   partial   class  TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SiteMapPath smp 
= new SiteMapPath();
smp.NodeTemplate 
= new MyTemplate();
form1.Controls.Add(smp);
}

}

Keep in mind, this code is for a single eval statement. If you wanted to add several controls, some styles, maybe another template or two (perhaps 20 lines of markup), this could quickly become 100+ lines of code for this single control. This is especially unfortunate because this is work that the parser can do reliably and quickly.

So the question is, how can I get the parser to parse the template markup for me and yet present me with a reference to the ITemplate object instead of consuming it?

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值