Thomas’ Developer Blog

September 7, 2008

Many to Many Repeaters Revisited

Filed under: Asp.Net Framework 2.0, sql — Tags: , , , , , , , — sanzon @ 8:24 pm

Well, after coming across a .net page requiring many to many repeaters, I decided to look back at my old solution. Generally, it worked, but it could of been done a LOT easier. The following is a very very simple way to complete many to many repeaters without using any unneeded methods.

<asp:SqlDataSource id="sdsParent" runat="server" 
    connectionstring="Your Connection String"
    providername="Your Provider Name"
    selectcommand="select parent_id from parent">
</asp:SqlDataSource>
<asp:Repeater id="rptParent" datasourceid="sdsParent" runat="server">
    <ItemTemplate>
        <asp:Label id="lblParent" runat="server" text=<%# DataBinder.Eval(Container.DataItem, "parent_id") %> />
            <asp:SqlDataSource id="sdsChild" runat="server" 
                connectionstring="Your Connection String"
                providername="Your Provider Name"
                selectcommand="select child_id from child where parent_id = ?">
                <SelectParameters>
                    <asp:ControlParameter Name="Parent" DefaultValue="1" Type="Int32" 
                         ControlID="lblParentID" PropertyName="Text" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:Repeater id="rptChild datasourceid="sdsChild" runat="server">
                <ItemTemplate>
                        <%# DataBinder.Eval(Container.DataItem, "child_id") %>
                </ItemTemplate>
            </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

That’s all there is to the code! Simply use a sql paramter in the datasource control to read from the lbl produced from the parent repeater. No codebehind code needed. Simply use your .net controls and you’ll be fine. Couldn’t be easier! Plus you don’t have to use those pesky public objects.. which I honestly think makes the code a lot more messy the old way.

Please note: You can make the label invisible if you want! As well include your own information from your current repeater, i shouldn’t need to exlain those basics though.

Good luck

Advertisements

2 Comments »

  1. Everything is very open with a very clear description of
    the challenges. It was really informative. Your site is very helpful.
    Thanks for sharing!

    Comment by how to configure best comcast dsl cable modem for xbox — August 31, 2014 @ 9:14 am

  2. Highly energetic post, I enjoyed that bit. Will there be a part
    2?

    Comment by how to configure best best comcast the best modem for xbox — September 4, 2014 @ 7:44 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: