Writing Content to HTML Using ArrayLists and Repeaters [ASP.NET]
I had to figure out how to perform just such a process for a project I’m currently working on. Being relatively new ASP.NET the first thing I did was head out to the internet to try and find a solid tutorial that would help me quickly figure out what I needed to do to get this done. For a process that is relatively straight forward and simple I found the absent of a good tutorial online surprising.
After spending a little more time than I would have liked to researching the best way to accomplish this, I did figure it out. I’m posting the steps I took here. Hopefully this can help somebody else out more quickly:
Overview: I’m collecting errors that my program is throwing and storing them into an array until the page is rendered and they can be displayed. I see through a lot of other tutorials that this is often done with string appending and catenation, but I find arrays easier to deal with. (Aside from the fact it’s a very appropriate use for them)
The first I had to do was set up a repeater in my mark up:
<!-- Repeater Setup Example --> <ul> <asp:Repeater ID="errorDisplayRepeater" runat="server"> <ItemTemplate> <li><%#Container.DataItem%></li> </ItemTemplate> </asp:Repeater> </ul>
I want my errors to be displayed within a list so I’m encapsulating the repeater with unordered list tags. The ItemTemplate tags indicates the html that I want repeated for each element contained within the array. The Container.DataItem object is a generic object that contains the data of datasource the repeater is bound to. In this case the container will be my array list and the data items will be the elements within it.
Turning to the code behind page: I want my error array to be available to my entire page, so I made my public declaration at the top of my code-behind page, just after the class declaration:
' ' Array List Declaration Example ' Partial Public Class myWebApplication Inherits System.Web.UI.Page Public errorArray as New ArrayList
As my application progresses it eventually enters into my edit checks function where the error messages are assigned. These don’t have to be in a function, I just prefer to break them out this way. I simply append the array as each conditional check fails:
' ' Error Collection Example ' If conditionOne <> "Met" Then errorArray.Add("Error! Condition One has not been met!") End If If conditionTwo <> "Met" Then errorArray.Add("Error! Condition Two has not been met!") End If ' etc...
Once I finished my error collection, I want to resize the array to represent the actual amount of errors I’ve collected and check the size to see if I’ve collected any at all. If I haven’t, I wanted to skip over binding the array to a repeater. Why waste the processing power if there is nothing to process? If there are elements within the array, I wanted to go ahead and bind them to the repeater:
' ' Error Length Check/DataBind Example ' errorArray.TrimToSize() If errorArray.ToArray.Length = 0 Then 'We're empty so the code for no action would be here ' In my case: Return False Else ' Remember errorDisplayRepeater is the name of the repeater errorDisplayRepeater.DataSource = errorArray errorDisplayRepeater.DataBind() Return True End If