[英]Repeater not creating child controls
我試圖找出為什么 DataBind() 確實在中繼器上觸發數據綁定事件,但沒有注入子控件 - ItemTemplate 標簽內的那些。
<asp:Repeater ID="rptMessages" runat="server">
<asp:ItemTemplate>
<VallisLive2:NotificationWidget ID="ctlNotification" runat="server"
NotificationID='<%# Eval("NotificationID") %>'
Title='<%# Eval("Title") %>'
Details='<%# Eval("Details") %>'
CreatedDateTime='<%# Eval("CreatedDateTime") %>'
Importance='<%# Eval("Importance") %>' />
</asp:ItemTemplate>
</asp:Repeater>
上面的結果是,雖然沒有錯誤,但中繼器內部什么也沒有出現。
我可以證明 DataSource 包含項目,如果我嘗試這樣做:
<asp:Repeater ID="rptMessages" runat="server" OnItemDataBound="rptMessages_ItemDataBound">
事件觸發並且數據值存在,但這證實了轉發器內的子控件沒有被創建,因為此問題的已知解決方法,手動設置值,如下所示:
protected void rptMessages_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
var n = e.Item.DataItem as Notification;
var control = e.Item.FindControl("ctlNotification") as NotificationWidget;
失敗: System.NullReferenceException: '未將對象引用設置為對象的實例。' 控制為空。 它在嘗試綁定它時無法在 Repeater 中找到該控件,因為它沒有被創建。
所以DataBind() 起作用了,它觸發了,它有有效的源數據,但最終的結果是Repeater 的輸出是空的,上面證明Repeater 沒有創建子控件。
任何想法都非常感謝:)
顯然昨天過得很糟糕。 答案是聲明而不是在Repeater里面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.