views:

462

answers:

5

Kind of related to my other question - I've only ever used HTMLControls with runat="server" and WebControls grudgingly, preferring to have control over the markup that gets generated (including the ids of the elements, etc.).

What's your suggestion for, say, iterating over the contents of a collection and generating a table or list without resorting to databinding or using Response.Write in a loop from the code-behind? I'm interested in the different approaches for creating clean, maintainable code.

+1  A: 

When you say "databinding," are you speaking of binding a database result set to a Gridview or Repeater, etc. via a .Bind() call, or just using any ASP.NET server control (or HTML server control) in general?

Because, if you just want to avoid using server controls in general, but don't want to use Response.Write either, you're seriously limited in your options.

Personally, if you want control over markup, why not just loop through a SqlDataReader or something and then save the results to a Literal control, using HTML where applicable. Then within the page (wherever you want the data to appear) just do:

 <asp:Literal ID="ltrResults" runat="server" />
I was talking mainly about the former as in my short experience binding to a repeater led to short term gain, but caused some pain as soon as I wanted to do any conditional formatting of the output, hook up jQuery events, etc. The literal approach sounds interesting - a bit nicer than R.W().
+3  A: 

There is nothing to stop you iterating over your collection directly in your aspx page.

 <ul>
     <% foreach(Person person in this.People) {%>

         <li><%=person.Firstname %> <%=person.Lastname %></li>

     <% } %>
 </ul>

In this example People is a list property on my codebehind. You will find many ASP.NET MVC projects are using this method.

Brownie
Maybe it hasn't been long enough but the pain of Classic ASP is still too fresh - putting too much of that (albeit) presentation logic in with the presentation markup itself is not for me.
Seems like the approach you are looking for would mean putting a lot of html in the code behind, which in my opinion, is also too much logic/presentation mixed together (just in the opposite way). That seperation is one of the strengths I see in databinding.
AJ
Get used to it. Even the (semi) new hotness Ruby/Rails does it this way. Welcome to the new boss, same as the old boss.
Jeff Atwood
A: 

@Brownie... yeah, but those are Response.Write statements... you're just using the shorthand format

I agree. nzduck said he didn't want to do it from the codebehind. This method still allows you to use the templating facilities of the WebForms view engine.
Brownie
A: 

Inspired by the first suggestion I've also tried adding a PlaceHolder to the aspx and then adding child controls to it programatically from the code-behind. I'm hoping I can create a user control for the repeating content and then add it to the PlaceHolder in a loop. This will allow the UI code to be nicely encapsulated and should hide all the StringBuilder action.

A: 

The repeater control is used for exactly what you want. It is a server control, but you specify what HTML is generated in the templates. You do databind, but isnt that just a shortcut for a manual loop?

Ben Dempsey