开发者

Setting complex members of a control inside a Repeater

TL;DR

The Control inside my Repeater requires special instantiation before it can be useful. How do I perform this special instantiation for each control as they are constructed by the repeater?

The Setup

I have three classes:

ComplexData.

Contains many public members of varying types, such as:

  • int favorite_integer
  • Food what_you_had_for_breakfast_this_morning
  • string capital_of_alaska
  • PhaseOfMoon when_magic_runes_will_reveal_themselves_on_the_mysterious_artifact

etc, etc, etc

ComplexDataDisplay

A control used to render an instance of ComplexData in an aesthetically pleasing way. Exposes a single public member, ComplexMember DataToDisplay. When the user sets DataToDisplay, the labels within the control will populate themselves with the appropriate data.

MultiComplexDataDisplay

A control that uses a Repeater to display multiple ComplexDataDisplays in a row. It contains a private List<ComplexData> datasToDisplay, which is the data source for the repeater. It provides two public methods, addComplexData(ComplexData) and emptyAllData(), which let the user manipulate datasToDisplay.

The Problem

During the data bind process, I don't know how to set each ComplexDataDisplay's DataToDisplay member.

开发者_JAVA技巧

It appears that controls within repeaters are normally populated from the front end, for example

<asp:Repeater runat="server" id="linkRepeater">
    <a href="<%# getUrl(Container.DataItem) %>"> <%# getDescription(Container.DataItem) %> </a>
</asp:Repeater>

As I understand it, during data bind, the repeater instantiates each anchor element, using whatever the data source is to set the href and description.

My attempt to replicate this behavior only led to a blank page:

<asp:Repeater runat="server" id="displayRepeater">
    <ComplexDataDisplay id="dataDisplay" DataToDisplay="<%# Container.DataItem %>" runat="server"
</asp:Repeater>

The only other thing I can think to try is to modify ComplexDataDisplay so it has a public member for each member of ComplexData. Then within the repeater I can do:

<asp:Repeater runat="server" id="displayRepeater">
    <ComplexDataDisplay id="dataDisplay" runat="server"
        favorite_integer="<%# get_favorite_integer(Container.DataItem) %>"
        what_you_had_for_breakfast_this_morning="<# get_what_you_had_for_breakfast_this_morning(Container.DataItem) %>"
        <%--etc etc etc--%>
    />
</asp:Repeater>

This seems highly undesirable because for every member of ComplexData, I'll have to write a corresponding public member in ComplexDataDisplay, and a get_whatever(DataItem) method in MultiComplexDataDisplay. On top of that, I don't even know if it will work, because half of ComplexData's members are complex data types, which may or may not be settable in this way.

What I'm looking for in an answer

One of the following:

  • A direct answer to the question posed in the TLDR section - a way to perform special instantiation for each control in the repeater during databind, or a way to iterate through them shortly after the fact.
  • A recommendation on how to best restructure the code, such so that special instantiation is no longer necessary to display my many Complex Datas. I am willing to add/update/delete any class besides ComplexData, as I am obligated to maintain its interface as-is.
  • best practices/references/documentation related to my problem, which will guide me in finding a solution myself.


You can bind you child controls inside of ItemDataBound, I couldn't follow which objects get bound when so replace where needed:

<asp:Repeater runat="server" id="displayRepeater" OnItemDataBound="displayRepeater_ItemDataBound">
    <uc1:ComplexDataDisplay id="dataDisplay" runat="server" />
</asp:Repeater>


 protected void displayRepeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
 {
      // Execute the following logic for Items and Alternating Items.
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
      {
         //get object bound to row
         ComplexData c = (ComplexData)e.Item.DataItem;
         //find the child control to bind
         ComplexDataDisplay cds = (ComplexDataDisplay)e.Item.FindControl("dataDisplay");
         //set properties or any other complex things you need to do
         cds.DataToDisplay = c.ComplexMember;
         cds.DataBind(); // if this control has repeaters, it's ItemDataBound will fire, repeat this process untill all your controls are bound properly
      }
   }    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜