如何通过数据回路的WebForms像MVC [英] How to loop through data in WebForms like in MVC

查看:87
本文介绍了如何通过数据回路的WebForms像MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何遍历的WebForms数据就像我在ASP.NET MVC吗?例如,在MVC,这很简单,只要:

How do I loop through data in WebForms like I do in ASP.NET MVC? For instance, in MVC, this is as simple as:

<table>
    @foreach (var myItem in g)
    { 
        @<tr><td>@MyItem.title<td></tr>
    }
</table>

什么是在WebForms的做到这一点的最简单,最简单的方法是什么?将在code背后是什么样子的?

What's the easiest and simplest way to do this in WebForms? What would the code behind look like?

或者,我可以添加一个MVC项目到web表单应用程序,这样我可以使用MVC功能,而不是?

感谢。

推荐答案

而不是使用一个中继器,你可以依次通过列表类似的MVC类型的方式使用&LT;%%&GT; &LT;%=%方式&gt; 标签

Rather than use a repeater, you can just loop through the list in a similar MVC type way using the <% %> and <%= %> tags.

<table>
  <% foreach (var myItem in g) { %>
    <tr><td><%= myItem.title %></td></tr>
  <% } %>
</table>

只要你通过循环的财产是从ASPX / ASCX页面acessible(例如或声明为受保护的公开),您可以通过它循环。有必要在后面的code无其他code。

As long as the property you're looping through is acessible from the aspx/ascx page (e.g. declared as protected or public) you can loop through it. There is no other code in the code behind necessary.

&LT;%%&GT; 将评估code和&LT;%=%&GT; 将输出结果。

<% %> will evaluate the code and <%= %> will output the result.

下面是最基本的例子:

在code。在你的职业等级声明这份名单的背后:

Declare this list at your class level in your code behind:

public List<string> Sites = new List<string> { "StackOverflow", "Super User", "Meta SO" };

这只是一个简单的字符串列表,以便然后在aspx文件

That's just a simple list of strings, so then in your aspx file

<% foreach (var site in Sites) { %> <!-- loop through the list -->
  <div>
    <%= site %> <!-- write out the name of the site -->
  </div>
<% } %> <!--End the for loop -->

这篇关于如何通过数据回路的WebForms像MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆