2014-10-08 62 views
0

看起來好像有幾個問題已經發布,但沒有一個能夠解決我的問題。我有一箇中繼器內的用戶控件。用戶控件只有一個公共屬性,只有一個getter/setter。數據綁定在初始頁面加載時發生得很好,但是當從下拉菜單中選擇過濾器時,第一個中繼器綁定沒問題,但包含用戶控件的第二個中繼器對於該屬性顯示爲空。因此,在實際代碼:中繼器中的ASP.NET用戶控件屬性在回發後爲空

我的兩個中繼器看起來像這樣:

<asp:Repeater ID="rptTransactionVisual" runat="server" OnItemDataBound="rptTransactionVisual_ItemDataBound"> 
      <ItemTemplate> 
       <%#Eval("TransactionAmount")%> 

^- 我有這個在完整性檢查 - 它總是得到一個值,即使在低於交易接收空值,所以我知道它得到一個數據源和數據綁定被 「RUNAT =」服務器」 />

<asp:Repeater ID="rptTransactions" runat="server" OnItemDataBound="rptTransactions_ItemDataBound" OnDataBinding="rptTransactions_DataBinding"> 
       <ItemTemplate> 
        <tr> 
         <td><%#DataBinder.Eval(Container.DataItem, "TransactionDate", "{0:MM/dd/yyyy}")%></td> 
         <td><asp:Literal ID="ltlTransactionAmount" runat="server" /></td> 
         <td><asp:Literal ID="ltlClient" runat="server" /></td> 
         <td><asp:Literal ID="ltlTransactionType" runat="server" /></td> 
         <td><asp:Literal ID="ltlSector" runat="server" /></td> 
        </tr> 
       </ItemTemplate> 
</asp:Repeater> 

我綁定的t值運算中繼到第二中繼器的數據源,因爲它們是相同的:

protected void rptTransactions_DataBinding(object sender, EventArgs e) 
    { 
     rptTransactionVisual.DataSource = rptTransactions.DataSource; 
     rptTransactionVisual.DataBind(); 
    } 

這裏是從用戶控制,最初工作正常相關的代碼,但是在濾波之後,事務始終爲空;

public Transaction transaction { get; set; } 

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

     if (transaction == null) return; 
.... 

下面是過濾代碼:我打我的頭靠在牆上這個

protected void ddTransactionType_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     var transactions = getChildPages().Where(p => p.TransactionType == ddTransactionType.SelectedValue).ToList<TransactionPage>(); 
     } 

     rptTransactions.DataSource = transactions; 
     rptTransactions.DataBind(); 
     //databinding happens always for the list repeater, and I do see the visual repeater being databound with the correct datasource 
    } 

回答

1

我仍然不知道爲什麼原始的一個在回發後是空的,但我確實通過對Visual repeater ItemDataBound事件進行了一些返工來解決它。

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
     { 
      TransactionPage transaction = (TransactionPage)e.Item.DataItem; 
      TransactionPagePartial visual = (TransactionPagePartial)Page.LoadControl("~/Views/Pages/Partials/TransactionPagePartial.ascx"); 
      visual.transaction = transaction; 
      rptTransactionVisual.Controls.Add(tombstone); 
     } 
+0

謝謝,這是正確的答案。您必須動態加載控件。 – 2016-05-17 14:40:20

相關問題