开发者

Code blocks in regular ASP.NET 4.0 aspx pages (not MVC)

Is it possible to do something like this in regular ASP.NET (or something similar):

<ul>
    <% foreach (var item in Model) { %>       
        <li id="<%: item.Id %>">    
            blah blah     
        </li>       
    <% } %>
</ul>    

I need to do a gird, but I want to control how the html table is output.开发者_StackOverflow


Yes, the code written this way behaves as if it was written in code behind. If you want to have both, that works too. Expose a property in the page code behind class that is the Model and then you can access it the way you have shown.

public partial class _Default : System.Web.UI.Page
{
    protected List<WhateverClass> Model { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Populate the model here or in another appropriate event
    }
}


It is possible since MVC just uses the ASP.NET rendering engine (where the <% server tags come from). However, you should probably use the repeater control as @Eric mentioned. The problem is that the Model may not be initialized at the time your page is compiled and you are likely to get a null reference exception.


Yes, you can do it, but when in web forms you should probably do things the "web forms way" (or else why use web forms).

In your example you can accomplish the same thing with a repeater.

Note that this stylistic advice; both approaches are 100% correct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜