Dennis Madsens Blog

14 May, 2009

ASP.NET: Nested repeater with LINQ DataSource

Posted by: Dennis Madsen In: ASP.NET

I’m working with the below MSSQL data structure which is handled by LINQ to SQL.

image

A ASP.NET WebService is using this LINQ to SQL to publish WebMethods like GetTanks() which returns a list of Tank objects.

My purpose is to create a web page which display the current SensorValues for all Tanks. Since I’m developing a client ASP.NET web site which use the WebService, I’m not able to use the LINQDataSource. Therefore I unfortunately can’t figure out how to use a GridView to display the values – instead I use a nested repeater.

Default.aspx:

<asp:Repeater ID="RepeaterTanks" runat="server">
    <HeaderTemplate>
        <table cellspacing="0" cellpadding="6" style="width:350px;">
            <tr>
                <th>
                    Tank
                </th>
                <th>
                    Sensor Type
                </th>
                <th>
                    Value
                </th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server"><strong><%# DataBinder.Eval(Container.DataItem, "Name")%>:</strong></asp:Label>
            </td>
            <td>
            </td>
            <td>
            </td>
        </tr>
        <asp:Repeater ID="RepeaterSensors" runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem, "Sensors")%>'>
            <ItemTemplate>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Label ID="Label3" runat="server"><%# DataBinder.Eval(Container.DataItem, "TypeName")%></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="Label4" ToolTip='<%# DataBinder.Eval(Container.DataItem, "ToolTip")%>'
                            runat="server">
                            <asp:Label ID="Label5" CssClass='<%# DataBinder.Eval(Container.DataItem, "CssClass")%>'
                                runat="server"></asp:Label>
                            <%# DataBinder.Eval(Container.DataItem, "Value")%>
                        </asp:Label>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

Default.aspx.cs

RepeaterTanks.DataSource = from tank in service.GetTanks()
                           select new {
                               Id = tank.Id,
                               Name = tank.Name,
                               Sensors = (from sensor in tank.Sensors
                                          where sensor.TankId == tank.Id
                                          orderby sensor.SensorTypeId
                                          let NewestValue = this.GetSensorValueFromService(sensor.Id)
                                          select new {
                                              TypeName = sensor.SensorType.Name,
                                              Value = NewestValue,
                                              WarningValue = sensor.WarningValue,
                                              ToolTip = "Warning value: " + sensor.WarningValue,
                                              CssClass = this.GetSensorValueCssClass((double)NewestValue, (double)sensor.WarningValue)
                                          })
                           };

RepeaterTanks.DataBind();

The problem was to create the LINQ DataSet (with a nested select) to display the data inside the nested repeater. The above source code results in the above table design:

image

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Google Bookmarks
  • email

Like it? Tweet it!

2 Responses to "ASP.NET: Nested repeater with LINQ DataSource"

1 | Tomas Crespo

April 15th, 2010 at 17:01

Avatar

Fantastic select! thank you

2 | Eran

June 19th, 2010 at 12:08

Avatar

Hello Desnnis,
I came a cross your post when googling on how to use nested repeaters with 3 tables in a scenario just like your with the Tank->Sensors->Sensor Value tables
and I can’t find the words to express my huge gratitude to you. you solved me 24 hours of searching in few minutes.

Thank you so much,
Eran

Comment Form

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

Categories

Twitter

    Tags

    Archives