Link to home
Start Free TrialLog in
Avatar of nuvium
nuvium

asked on

Dynamically creating a table within a repeater based on rows returned

Hi. I'm sure this question has been answered before around here, but maybe I just can't find the keywords that will correctly get me to it, so Im going to pose my question anyway.

I have a repeater which is dynamically populated via databinding from a stored procedure. This stored proc will ALWAYS pull back the same data per row, but the amount of rows can vary. I basically need some help with the logic to create a table dynamically within the repeater to house the returns from the stored proc, but so that it can handle a total of 3 items per row, or as little as 1. Just in case that was a little vague, I want a table with 3 cells per row max, but able to house 1 or 2 if there are only 1 or 2 returns from the query.

Below is what I have so far (you'll notice the rows and cells are missing from within the repeater). I am also pasting my databind code from the backend as well. Help!!

ASPX ---------------------
<table border="0" cellspacing="0" cellpadding="0" width="100%">
                         <tr>
                           <td>
                            <table border="0" cellspacing="0" cellpadding="0" width="100%">
                            <asp:Repeater ID="rptProductsList" runat="server">
                            <ItemTemplate><div class="ProductTitle"><%#Eval("ProductTitle")%> (<%#Eval("ProductModelNumber")%>)</div><br />
                            <a onclick="MM_openBrWindow('showPic.aspx?imageID=<%#Eval("ImageID") %>','','resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,width=600,height=700'); return false;" href="#">
                            <img src=<%#Eval("ImagePhysicalLocation")%>th<%#Eval("ImageFileName")%> border="0" /></a>
                            </ItemTemplate>
                            </asp:Repeater>
                            </table>
                            </td>
                         </tr>
                        </table>

VB-----------------
 Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("Conn").ConnectionString)

        Dim cmd As New SqlCommand("uspGetProductsFromSubCat", cn)
        cmd.CommandType = Data.CommandType.StoredProcedure
        cmd.Parameters.Add(New SqlParameter("@ProductCategoryID", Data.SqlDbType.Int))
        cmd.Parameters("@ProductCategoryID").Value = Session.Item("currentSubCatID")
        cn.Open()
        rptProductsList.DataSource = cmd.ExecuteReader()
        rptProductsList.DataBind()
        cn = Nothing
Avatar of Solar_Flare
Solar_Flare

are you trying to do something like this?
<asp:repeater ID=xx runat=server>
<HeaderTemplate>
   <table border="0" cellspacing="0" cellpadding="0" width="100%">
</HeaderTemplate>
<FooterTemplate>
   </table>
</FooterTemplate>
<ItemTemplate>
   <tr>
          <td>        
                  <div class="ProductTitle"><%#Eval("ProductTitle")%> (<%#Eval("ProductModelNumber")%>)</div>
          </td>
          <td>
                  <a onclick="MM_openBrWindow('showPic.aspx?imageID=<%#Eval("ImageID") %>','','resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,width=600,height=700'); return false;" href="#"><img src=<%#Eval("ImagePhysicalLocation")%>th<%#Eval("ImageFileName")%> border="0" /></a>
           </td>
     </tr>
</ItemTemplate>
 
</asp:repeater>

Open in new window

Avatar of nuvium

ASKER

You have the right idea, but my needs are a little bit more in depth. I understand using the header and foorter templates to open and close the table, but basically I need the table to have three cells per row (MAX). Each cell will contain a single row return from the database (this includes an image, and some description info). After three of these cells, the row for the table is done, and a new row starts.

Now, using what you showed me above, it looks like you have one row of the database return per table row on the front end, with two cells, one with the description info, one with the image. You understand the difference between what I need and what you showed me?

If it helps at all, I've done this before (which is why im kicking my own ass over forgetting the logic). I remember somone showed me how to do it, and I had to do some logic on the backend that basically dynamically created cells and open/close <tr>'s based on whether a) there have been 3 rows returned from the stored proc (one row goes into each of the three maximum cells per table row on the front end). I believe the logic centered around whether or not it was an even or odd count as to how to handle the creation.

This all sounds much more complicated than it is. I basically just need the repeater to dynamically create a table which has 3 cells per row maximum, each cell housing a full row return from the database. The only catch is, in some instances, only one or two rows will be returned (thus not even having a need for three cells). The table needs to be smart enough not to break when this happens, or populate the remaining cells with empty space.

Let me know if I can explain this better.
ASKER CERTIFIED SOLUTION
Avatar of photowhiz
photowhiz
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, EE's wonderful Code Snippet box totally messed that up. Just ignore the code box, and let me know if the reply doesn't make sense.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin