For reasons that are probably not worth mentioning in this post, I have decided to stop using ASP.NET controls and simply use regular HTML controls for my .aspx pages. As such, to dynamically generate HTML, I use c# inline to the .aspx to do what I need to do.
For example: this .aspx snippet shows how I am dynamically creating a <select> element where the <option> elements are driven by looping through a generic list of objects.
<select name="s">
<option value="-9999">Select an entity...</option>
<% foreach (MyEntity e in this.MyEntities)
{%>
<option <% if (MyEntityInScope.ID == e.ID)
{ %>selected<%} %> value="<%= e.ID %>">
<%= e.Name%></option>
<%} %>
</select>
Functionality-wise, I prefer this method of creating HTML (I feel more in control of how the HTML is generated vs ASP controls). However, syntactically (and visually), I think it's cumbersome (and ugly).
Is there a "better" way (another syntax) to dynamically generate HTML w/out resorting to using ASP.NET controls?
发布评论
评论(3)
为什么不把逻辑放入一个方法中并调用这个方法呢?
Why don't you put your logic into a method and call this method?
一种常见的方法是通过 XSLT 实现 XML。也就是说,您的代码组装 XML 文档、加载合适的 XSLT 转换并发送结果。
A common approach is XML through XSLT. That is, your code assembles an XML document, loads a suitable XSLT transform and sends the result.
返回 HTML 字符串的实用程序方法可以帮助解决此问题,类似于 ASP.NET MVC 中的 HTML 帮助程序。
A utility method that returns an HTML string can help with that, similar to the HTML helpers in ASP.NET MVC.