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

Earlier I was running Windows 7 Beta (build 7000) on both my 64-bit desktop (Intel I7-920 quad-core setup) and 32-bit netbook (Asus Eee 1002HA). As I’m unfortunately not a MSDN-member I have to wait until the 5th of May to get hold of the new Windows 7 RC (build 7100). Yesterday I had time for installing this – but not time enough to make a complete reinstallation including configuring all settings and reinstalling every single program afterwards. Therefore I decided to use the “upgrade” feature for the first time. After downloading the ISO-files from Microsoft Download I simply extracted it with WinRAR and ran the setup.exe-file directly from Windows. Remember to choose upgrade in the installation setup. After about an hour the upgrade on the desktop computer was complete. Afterwards all my programs still work and all my stuff is where it should be. Anyway, Remember to take backup of your stuff before upgrading!

Performance

I’m using PerformanceTest by PassMark to benchmark my system. Before upgraded to Windows 7 Beta my score was 1554,4 points. Afterwards it got 1750 in the same test. A performance increase of ~12%. Awesome!

Tags: