How do I create HTML output without a) data binding or b) using Response.Write?

Kind of related to my other question - I've always used HTMLControls with runat = "server" and WebControls reluctantly, preferring to have control over the markup that's (including element ids, etc.).

What do you suggest, say, iterate over the contents of a collection and generate a table or list without resorting to data binding or using Response.Write in a loop from code? I am interested in different approaches to creating clean, maintainable code.

-1


source to share


5 answers


When you say data binding, you are talking about binding the result to the database to a Gridview or Repeater, etc. by calling .Bind () or just using any ASP.NET server control (or HTML server control) in general?

Because if you just want to avoid using server controls altogether, but don't want to use Response.Write, then you are severely limited in your settings.



Personally, if you want to control the markup, why not just skip the SqlDataReader or whatever, and then store the results in a Literal control using HTML where applicable. Then on the page (wherever the data is) just do:

 <asp:Literal ID="ltrResults" runat="server" />

      

+1


source


There is nothing that prevents you from iterating over your collection directly on 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 in my code language. You will find many ASP.NET MVC projects using this method.

+4


source


@Brownie ... yes, but these are Response.Write statements ... you just use the shorthand format

0


source


Inspired by the first suggestion, I also tried to add the PlaceHolder to the aspx and then programmatically add the child controls from the code. I hope I can create a custom control for repeating content and then add it to the PlaceHolder in a loop. This will intuitively encapsulate the UI code and hide all StringBuilder actions.

0


source


The repeater control is used exactly the way you want. This is a server-side control, but you specify what HTML is generated in the templates. You are doing data binding, but isn't that just a shortcut for a manual loop?

0


source







All Articles