Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

hope you are well. and thank you in advance for any help you can give me on this.

I have a system, that requires a repeater created (with a new header) based on a value returned from the sql query

for example my table holds information like follows:

Field 1 | Field 2 | field 3
info 1 info 1
info info 1
info info 2
info info 2
info info 2

any idea how i would create a repeater dynamically (programmably) during runtime based on the contents of my data in database table

creating a repeater at runtime for all infomration or some is easy, but dont know how to create another one if field 3 changes value 2 - so for all entries with value 2 place in a repeater, all entries with value 1 create them into a repeater


hope this is coming across correctly. but since tomorrow there could be 6 entires with field value of 4, i want to it do the same again and group the 4's together..

so in other words create a repeater for every group of entiries with same value in field3

Front end code looks like follows:
<asp:Repeater ID="SKillsTOEnter" runat="server">
     <HeaderTemplate>
         <table width="50%">
             <tr>
                 <th>Field1</th>
             </tr>
     </HeaderTemplate>
     <ItemTemplate>
         <tr>
             <td width="80%"><Asp:label id="fieldinfo" Text=<%#DataBinder.Eval(Container.DataItem, "Field1")%> runat="server"></Asp:label></td>
                </tr>
     </ItemTemplate>
     <FooterTemplate>
         </table>
     </FooterTemplate>
 </asp:Repeater>


Thanks in advance
Robert Belsten

What I have tried:

i can create a single asp repeater to pull them all , but not one to add a header when field 3 changes value or create a new repeater when value changes in field 3

Backend code

myCommand = New SqlDataAdapter("select Field1, field2, FIeld3 From Table1  Order By FIELD2 ", myConnection)
        myCommand.Fill(ds)
        SKillsTOEnter.DataSource = ds
        SKillsTOEnter.DataBind()
Posted
Updated 14-Jun-17 9:56am
Comments
ZurdoDev 14-Jun-17 10:37am    
Just get your data coming out in the order and way you want.
Member 12758480 14-Jun-17 10:54am    
thanks for that,

however, i wish it was that simple, every time field 3 changes it is a different topic... and therefore the header of the repeater is different . and i could just create a repeater for each value, but tomorrow a new topic could be created .


ZurdoDev 14-Jun-17 10:57am    
Each repeater item represents a row from your data. So, get your data correct and then doing the repeater is the easy part. Or, are you saying you need a nested repeater? And if so, that's still relatively easy.
Member 12758480 14-Jun-17 11:10am    
yep so there are about 10 items that have number 1 in field 3 in my table , but that can be chanegd at any time which means the repater grows, thats easy, but how do you get asp, to create a new repeater when that value in field 3 to 2 (and there are 5 rows with value 2 of which can also change)

so create a new repeater at runtime if it finds a new value under field 3 in the table

hope tha helps as finding it difficult to explain myself lol..

1 solution

How about something like this:
VB.NET
SkillsToEnter.DataSource = ds.Tables(0).GroupBy(Function (row) row.Field(Of Integer)("Field3"))
SkillsToEnter.DataBind()

<asp:Repeater id="SkillsToEnter" runat="server">
<ItemTemplate>
    <h1>Field3: <%# Eval("Key") %></h1>
    
    <asp:Repeater runat="server" DataSource='<%# Container.DataItem %>'>
    <HeaderTemplate>
        <table style="width:50%">
        <thead>
            <tr>
                <th>Field1</th>
            </tr>
        </thead>
        <tbody>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td style="width:80%">
                <asp:label id="fieldinfo" runat="server" Text='<%# Eval("Field1") %>' />
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </tbody>
        </table>
    </FooterTemplate>
    </asp:Repeater>
</ItemTemplate>
</asp:Repeater>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900