1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Dynamically set the contents of list control

Dynamically set the contents of list control

2 posts, 0 answered
  1. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    29 Jul 2009
    Link to this post
    Hello all, question:

    I have a FAQ page that utilizes a ListDisplay control to output the data. I want to be able to modify the contents of each item in the list on the fly. The reasoning for this is some users do not have full access and I want to display a message instead of the FAQ answer telling them they must upgrade their account in order to see the answer. So far, I have tried the following:

    1 protected void Page_Load(object sender, EventArgs e) 
    2
    3     lists.DataBinding += new EventHandler(lists_DataBinding); 
    4
    5  
    6 void lists_DataBinding(object sender, EventArgs e) 
    7
    8     var oRepeater = (Repeater) lists.FindControl("listItems"); 
    9  
    10     oRepeater.DataBinding += new EventHandler(oRepeater_DataBinding); 
    11

    I get a null reference exception on line 10, since the second repeater inside the control is apparently not accessible by these means. Here is the ControlTemplate I am using for the ListDisplay:

    1 <asp:Repeater ID="lists" runat="server"
    2     <ItemTemplate> 
    3         <h2 class="sf_listTitle"><asp:Literal ID="listTitle" runat="server"></asp:Literal></h2
    4          
    5         <class="sf_controlListItems"
    6             <id="expandAll" runat="server"
    7                 <asp:Literal ID="Literal1" runat="server" Text="<%$Resources:ExpandAll %>"></asp:Literal> 
    8             </a> 
    9             <id="collapseAll" runat="server" style="display:none;"
    10                 <asp:Literal ID="Literal2" runat="server" Text="<%$Resources:CollapseAll %>"></asp:Literal> 
    11             </a> 
    12         </p> 
    13          
    14         <ul class="sf_expandableList" id="expandableList" runat="server"
    15             <asp:Repeater ID="listItems" runat="server"
    16                 <HeaderTemplate> 
    17                 </HeaderTemplate> 
    18                 <ItemTemplate> 
    19                     <li> 
    20                         <h1 id="headlineContainer" runat="server" class="sf_listItemTitle"
    21                             <id="headline" runat="server" title="<%$Resources:ClickToExpand %>"></a> 
    22                         </h1> 
    23                         <div id="contentContainer" runat="server" class="sf_listItemBody" style="display:none;"
    24                             <asp:Literal ID="content" runat="server"></asp:Literal> 
    25                         </div> 
    26                     </li> 
    27                 </ItemTemplate> 
    28                 <FooterTemplate> 
    29                 </FooterTemplate> 
    30             </asp:Repeater> 
    31         </ul> 
    32     </ItemTemplate> 
    33 </asp:Repeater> 

    The bottom line is I need to be able to gain programmatic access the literal control 'content' inside of 'listItems'.

    Any suggestions? Thanks!
  2. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    29 Jul 2009
    Link to this post
    Solution:

    void lists_ItemDataBound(object sender, RepeaterItemEventArgs e) 
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) 
        { 
            var oRepeater = e.Item.FindControl("listItems"); 
     
            foreach (RepeaterItem oItem in ((Repeater)oRepeater).Items) 
            { 
                if (oItem.ItemType == ListItemType.AlternatingItem || oItem.ItemType == ListItemType.Item) 
                { 
                    Literal oContent = (Literal)oItem.FindControl("content"); 
                    oContent.Text = "Something New"
                } 
            } 
        } 

    Maybe this can be helpful to someone :)
2 posts, 0 answered