Jamie Hartnoll
Jamie Hartnoll

Reputation: 7341

Creating a table and binding to repeater dynamically from two database tables

I am working on a CMS system for my business, and need to be able to generate notifications dynamically into a control panel. Please note, I am using .NET 2.0, coding with VB and my data is all held on a MySQL database.

This is pretty simple for most aspects, but as the whole thing is dynamic some things are proving more difficult.

I have a set of

tags which are dynamically generated with a repeater as follows:

<asp:Repeater runat="server" ID="locationRepeater">
<ItemTemplate>
    <p id='locationNotification' title="<%# Container.DataItem %>" runat='server'>
           COUNT DATA NEEDS TO GO HERE
    </p>                                
    <div class="jewelDescription" id="locationQuestionsNote" runat='server'>
       Notification pending for <%# Container.DataItem %> <!-- (gives location name) -->
    </div>
</ItemTemplate>
</asp:Repeater>

Currently, the Container.DataItem gives me the name of the location from a very simple dataBind of a list of location names

Dim locationList As Array = Split("1,2,3", ",")
    locationRepeater.DataSource = locationList
    locationRepeater.DataBind()

This is great, it allows locations to be added and removed from the system, the control panel is automatically updated depending on the number of locations currently in use.

HOWEVER... I've not got the actual data here, which should be a count of records from a separate Database table.

The data is really, really, simple, all I need to be able to do is query a table on my MySQL database for COUNT of ID WHERE locationName = Container.DataItem during each loop of the repeater... but I don't know how to do this.

I'm thinking maybe I have to do this first, create a table in memory somehow, but I am afraid I have no idea how to achieve this.

Has anyone got any examples of a similar thing, it's kind of binding to a repeater from two datasources... each task is very simple, but I don't know how to put it all together!

Upvotes: 3

Views: 2024

Answers (1)

rick schott
rick schott

Reputation: 21117

You can use the Repeater.ItemDataBound event:

<asp:Repeater runat="server" ItemDataBound="locationRepeater_ItemDataBound" ID="locationRepeater">

 Protected Sub locationRepeater_ItemDataBound(Sender As Object, e As RepeaterItemEventArgs)

    If (e.Item.ItemType = ListItemType.Item) Or _
        (e.Item.ItemType = ListItemType.AlternatingItem) Then

        'EXAMPLE: Change this to get your id and do the database call.
        If CType(e.Item.DataItem, Evaluation).Rating = "Good" Then
            CType(e.Item.FindControl("RatingLabel"), Label).Text = _
                "<b>***Good***</b>"
        End If
    End If
 End Sub

Upvotes: 1

Related Questions