Nested ASP Repeaters without a line of code

Recently I came across a little task, which required listing a few question topics with all of its questions. Since there was only a few topics and each topic have only few questions thought I will solve that with nested repeater controls, combined with LinqDataSource. Whit that I didn’t needed to write a single line of code in code behind, just markups. This makes the code behind much clearer, and it’s easy to understand in a blink of an eye.

Here is an example using the good old Northwind sample database.

<asp:Repeater ID="rptCategories" runat="server" DataSourceID="ldsCategories">
            <div style="background-color: Navy; color: White; font-weight: bold;">
                <asp:Label ID="lblCategoryName" runat="server" Text='<%# Eval("CategoryName") %>' />
            <asp:HiddenField ID="hfCoategoryId" runat="server" Value='<%# Eval("CategoryID") %>' />            
            <asp:Repeater ID="rptProducts" runat="server" DataSourceID="ldsProducts">
                    <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>' />
                    <asp:HiddenField ID="hfProductID" runat="server" Value='<%# Eval("ProductID") %>' />
                    <br />
            <asp:LinqDataSource ID="ldsProducts" runat="server" 
            ContextTypeName="WebRepeater.NorthwindEntities" TableName="Products" 
            Select="new(ProductName, ProductID)" Where="CategoryID=@CatID">
                    <asp:ControlParameter ControlID="hfCoategoryId" Type="Int32"
                     DefaultValue="1" Name="CatID" />
            <br />
            <br />
    <asp:LinqDataSource ID="ldsCategories" runat="server" TableName="Categories"
    ContextTypeName="WebRepeater.NorthwindEntities" Select="new(CategoryID, CategoryName)" />

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s