I have a small repeater which loops through up to about 10 items in a database, and does database calls within each repeat to get the values (Numerical) required to enter into a <p> tag in the HTML.
Once the repeater has completed I need to get the sum of all the <p> tag values.
My repeater code is
<asp:Repeater runat="server" ID="locationRepeater" OnItemDataBound="getQuestionCount">
<ItemTemplate>
<p class='mainNotifications clickTip' id='locationQuestions' runat='server'>/p>
</ItemTemplate>
</asp:Repeater>
The repeater is, as mentioned, looped about 10 times from a list. The function called by OnItemDataBound is a simple mySQL query with a single numerical output, this output is entered into the <p> tag, with this code:
CType(e.Item.FindControl("locationQuestions"), HtmlGenericControl).InnerHtml = outputFromMySQL
So what I end up with is 10 <p> tags, each with a numerical value.
Using jQuery I could count the values by summing the totals with .each('.className') and .val() but I want to be able to do this with code behind.
I guess… basically, what I need to do is make a summation within the repeater’s loop and then output it to the total, but I don’t know how to do that!
Why are you executing the query for each repeater item? You would get a much better performance if you would fetch the data upfront in one query. Then you could also easily calculate the total value.
But if this is what you really want you could always add a ‘TotalCount’ instance variable to your code behind class and on each OnItemDataBound increment this value with the result of your query.
Then when you’re done databinding the repeater you will have the total value in the instance variable