-1

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.

Community
  • 1
  • 1
nzduck
  • 476
  • 4
  • 12
  • This question seems a little bizarre to me. Ultimately something has to write to the response else you'll send nothing to the client. Can you clarify, why would you want to avoid Response.Write? – AnthonyWJones Jan 01 '09 at 22:00
  • Obviously I want to add to the response, just not using "Response.Write()" if there's a better way (which is what I'm asking). – nzduck Jan 03 '09 at 06:16

5 Answers5

4

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
  • 7,688
  • 5
  • 27
  • 39
  • 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. – nzduck Jan 03 '09 at 00:00
  • 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. – Abram Simon Jan 03 '09 at 06:15
  • 1
    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 Jan 03 '09 at 07:02
1

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(). – nzduck Jan 02 '09 at 23:58
0

@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 Jan 01 '09 at 23:03
0

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.

nzduck
  • 476
  • 4
  • 12
0

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
  • 360
  • 1
  • 6