如何在不使用 a) 数据绑定或 b) 使用 Response.Write 的情况下创建 HTML 输出?

发布于 2024-07-11 06:29:31 字数 322 浏览 14 评论 0原文

与我的其他问题有关 -我只勉强使用过带有 runat="server" 的 HTMLControls 和 WebControls,更喜欢控制生成的标记(包括元素的 id 等)。

例如,您对迭代集合的内容并生成表或列表而不诉诸数据绑定或在代码隐藏循环中使用 Response.Write 有什么建议? 我对创建干净、可维护代码的不同方法感兴趣。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

渔村楼浪 2024-07-18 06:29:31

没有什么可以阻止您直接在 aspx 页面中迭代集合。

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

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

     <% } %>
 </ul>

在此示例中,People 是我的代码隐藏中的列表属性。 你会发现很多ASP.NET MVC项目都在使用这种方法。

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.

残月升风 2024-07-18 06:29:31

当您说“数据绑定”时,您是指通过 .Bind() 调用将数据库结果集绑定到 Gridview 或 Repeater 等,还是通常使用任何 ASP.NET 服务器控件(或 HTML 服务器控件)?

因为,如果您只是想避免一般情况下使用服务器控件,但也不想使用 Response.Write,那么您的选择就受到严重限制。

就个人而言,如果您想控制标记,为什么不直接循环遍历 SqlDataReader 或其他东西,然后将结果保存到 Literal 控件,在适用的情况下使用 HTML。 然后在页面内(无论您希望数据出现在哪里)只需执行以下操作:

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

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" />
素衣风尘叹 2024-07-18 06:29:31

@Brownie...是的,但这些是 Response.Write 语句...您只是使用速记格式

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

幸福还没到 2024-07-18 06:29:31

受第一个建议的启发,我还尝试将 PlaceHolder 添加到 aspx,然后从代码隐藏中以编程方式向其添加子控件。 我希望我可以为重复内容创建一个用户控件,然后将其循环添加到 PlaceHolder 中。 这将使 UI 代码得到很好的封装,并且应该隐藏所有 StringBuilder 操作。

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.

前事休说 2024-07-18 06:29:31

中继器控件正是用于您想要的用途。 它是一个服务器控件,但您可以指定在模板中生成什么 HTML。 您进行数据绑定,但这不是手动循环的快捷方式吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文