Home » Asp.netRSS

Data Binding

Hey All,

I think this should be a relatively simple issue, but I cannot figure it out or find a good example.  I have a repeater nested within a repeater.  The outer repeater is databound to a list of data sources.  The inner repeater displays has table logic to display each data source (of the 'DataSource' class).  Here is the code:


<asp:Repeater ID="repeaterOuter" runat="server">
    <HeaderTemplate><table class="inputTbl"></HeaderTemplate>
    <FooterTemplate></table>End List</FooterTemplate>

    <ItemTemplate>
        <tr class="headerRow"><td colspan="10">Group Name Here</td></tr>
        <asp:Repeater DataSource="<%# Container.DataItem %>" runat="server">
            <HeaderTemplate>
            
                <tr class="headerRow"><th>Name</th><th>Crew Position</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th>
                <th>Thursday</th><th>Friday</th><th>Saturday</th><th>Sunday</th><th>Remarks</th>
                </tr>
            </HeaderTemplate>
            <FooterTemplate></FooterTemplate>
            <AlternatingItemTemplate>
            <ItemTemplate>
                <tr class="colorRow"><td><%# Eval("Name") %></td><td><%# Eval("Position") %></td> <td><%# Eval("Monday") %></td>
                <td><%# Eval("Tuesday") %></td><td><%# Eval("Wednesday") %></td><td><%# Eval("Thursday") %></td>
                <td><%# Eval("Friday") %></td><td><%# Eval("Saturday") %></td><td><%# Eval("Sunday") %></td>
                <td><%# Eval("Remarks") %></td>
                </tr>
            </ItemTemplate>
            </AlternatingItemTemplate>
            <ItemTemplate>
                <tr class="normRow"><td><%# Eval("Name") %></td><td><%# Eval("Position") %></td> <td><%# Eval("Monday") %></td>
                <td><%# Eval("Tuesday") %></td><td><%# Eval("Wednesday") %></td><td><%# Eval("Thursday") %></td>
                <td><%# Eval("Friday") %></td><td><%# Eval("Saturday") %></td><td><%# Eval("Sunday") %></td>
                <td><%# Eval("Remarks") %></td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
        
    </ItemTemplate>

</asp:Repeater>

Within the <ItemTemplate> of the outer repeater, but outside of the inner Repeater, I'd like to pull a value that is in the table of any row of the active DataSource- specifically cell saying "Group Name Here".  So in other words, I have a DataSource, with a column labeled "Group", what is the databinding expression to reach into the first row of this data source to get this group name?

Does this make sense?  Can anyone help?  Thanks!


 

4 Answers Found

 

Answer 1

Hi,

First bind the repeatror 1 and then  find  the repeater2 and then bind it...

 

Answer 2

The code  as written works.  I just don't know the proper data  binding expression  to get the group  name extracted from the data source  in the cell  stating "Group Name Here".  The bound and nested  repeaters otherwise work perfectly.

 

Answer 3

two ways to do it, declaratively and the codebehind using the ItemDataBound event. 

though for both you would have to use some webcontrol to set a value to. 


<asp:Repeater ID="repeaterOuter" runat="server">
    <HeaderTemplate><table></HeaderTemplate>
    <FooterTemplate></table>End List</FooterTemplate>
    <ItemTemplate>
        <tr><td colspan="10"><asp:Label ID="Label1" runat="server" Text="" /></td></tr>

1. declaratively:

<ItemTemplate>
                <%# ((Label)((RepeaterItem)Container).NamingContainer.Parent.FindControl("Label1")).Text = Convert.ToString(Eval("Group"))%> 
                <tr><td><%# Eval("Name") %></td><td><%# Eval("Position") %></td> <td><%# Eval("Monday") %></td>

2.codebehind:

* ps. give an ID to the nested  Repeater, and wire its ItemDataBound event 

<asp:Repeater ID="RepeaterInner" DataSource="<%# Container.DataItem %>" runat="server" OnItemDataBound="RepeaterInner_ItemDataBound">

and in the ItemDataBound event handler:

        protected void RepeaterInner_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Label lbl = ((RepeaterItem)e.Item.NamingContainer.Parent).FindControl("Label1") as Label;
                if (lbl != null)
                {
                    if (string.IsNullOrEmpty(lbl.Text))
                    {
                        lbl.Text = DataBinder.Eval(e.Item.DataItem, "Group").ToString();
                    }
                }
            }
        }



 

Answer 4

That worked perfectly.  A little bit different than the angle I was taking- thanks for the help.

 
 
Search for  Data Binding
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter