0
我正在嘗試爲我的服務器控件(標題模板和內容模板)創建模板。創建asp.net服務器控件模板
<uc:MyControl ID="myConrol1" runat="server">
<CaptionTemplate>
<%# Eval("MyCaption") %>
</CaptionTemplate>
<ContentTemplate>
<b><%# Eval("MyContent") %></b>
</ContentTemplate>
</uc:MyControl>
然後再給與數據源的模板
var ds = new List<CarouselItem>
{
new CarouselItem()
{
MyCaption = "Slide 1 caption",
MyContent = "Slide 1 content"
},
new CarouselItem()
{
MyCaption = "Slide 2 caption",
MyContent = "Slide 2 content"
}
};
myConrol1.DataSource = ds;
myConrol1.DataBind();
但是我已經試過沒有爲我工作。以下是我試圖實現此功能的代碼。
[Browsable(false), DefaultValue(null), Description("The content template."), TemplateContainer(typeof(MyItemContainer)), PersistenceMode(PersistenceMode.InnerProperty)]
public virtual ITemplate ContentTemplate { get; set; }
[Browsable(false), DefaultValue(null), Description("The caption template."), TemplateContainer(typeof(MyItemContainer)), PersistenceMode(PersistenceMode.InnerProperty)]
public virtual ITemplate CaptionTemplate { get; set; }
然後我創建
public class MyItemContainer : WebControl, INamingContainer
{
private readonly MyItem item;
public MyItemContainer (MyItem ai)
{
item = ai;
}
public MyItem DataItem
{
get { return item; }
}
}
然後在綁定的方法寫InstantiateIn這一類
foreach (object dataSourceObject in ds)
{
var item = new MyItem
{
Caption = new PlaceHolder(),
Content = new PlaceHolder(),
DataItem = dataSourceObject
};
if (ContentTemplate != null)
{
MyAccordionItemContainer cc = new MyAccordionItemContainer(item);
ContentTemplate.InstantiateIn(cc);
item.Content.Controls.Add(cc);
}
if (CaptionTemplate != null)
{
MyAccordionItemContainer cc = new MyAccordionItemContainer(item);
CaptionTemplate.InstantiateIn(cc);
item.Caption.Controls.Add(cc);
}
item.DataBind();
}
頁無一切都是空的,而不是綁定。似乎我沒有在實例化過程中使用dataSourceObject
,但我不確定如何使用他。有沒有人有任何想法嗎?