Archive for 'ASP.NET'
Display Attribute Not Working With ASP.NET MVC 2 RTM
Published on August 25th, 2010.
Recently I was asked about a strange problem concerning the Display attribute in ASP.NET MVC 2. The problem was the Display atribute had no effect in the view. It was really a strange one. I tried it myself and it was true.
Let’s have a look at that situation. Below is the model for a user.
public class User { [ScaffoldColumn(false)] public int Id { get; set; } [Required] [Display(Name = "User name")] public string Name { get; set; } }
Using Html.DisplayForModel() in our view results in the following web page:
As you can see the Display attribute has not been taken into consideration at all. The reason for this is ASP.NET MVC 2 RTN do not know about it. Display attribute is new for .NET 4 and ASP.NET MVC 2 RTM is compiled under .NET 3.5. This attribute is supported in Futures release of ASP.NET MVC and will be supported in any newer releases (like MVC 3).
OK, but I didn’t give you a working solution so far. Well, there is – DisplayName attribute. Note that it resides in System.ComponentModel not in System.ComponentModel.DataAnnotations. Our model now looks like this:
public class User { ... [Required] [DisplayName("User name")] public string Name { get; set; } }
Filled under ASP.NET MVC. No Comments.
ASP.NET MVC DI with Common Service Locator and NInject
Published on August 5th, 2010.
I think everyone knows about Dependency Injection (DI) and how important it is when it comes to building low-coupled components. There are many IoC (Inversion of Control) containers. The most popular are
Each of them has its own pros and cons. I have been using NInject for years and I am really happy with it. Autofac is quite a new DI framework which shows really good results on benchmarks. You can find information for all DI frameworks on their websites or just google them for comparison. (more…)
Filled under ASP.NET, Design Patterns, Development Techniques. 1 Comment.
Test-Driven Development with ASP.NET MVC
Published on August 3rd, 2010.
ASP.NET MVC has become more and more popular. In my previous post I gave you my vision about it and what it introduced to ASP.NET developers. One of the advantages of the MVC pattern is the better testability. You can easily test every controller without dealing with the View itself.
Test-Driven Development (TDD)
Principles of TDD are really simple. The entire TDD process is described on the following scheme:
Filled under ASP.NET, Design Patterns, Development Techniques, Testing. No Comments.
Why is ASP.NET MVC so Popular?
Published on August 2nd, 2010.
Classic ASP.NET
ASP.NET MVC has become more and more popular. Why? The ordinary ASP.NET allows developers to abstract from many details like request/response headers (by wrapping everything in nice API), manual HTML coding (by using server-side controls), etc. Everything is really cool – we develop a Web site for seconds. But what about the maintainability of this site later? Customers always want more and more. Even after years they may ask you to implement a new feature for them. And then developers start to sink into the depth of their own code ocean. If we add the lack of good documentation, the entire situation becomes really bad. (more…)
Filled under ASP.NET, Design Patterns. 1 Comment.
Capture the HTML output from a custom server control in ASP.NET
Published on November 20th, 2008.
Custom server controls do great job. A complex page can easily be split into many controls. In this way it becomes easier to support after that. Used in a page, a server control follows the whole page life cycle.
Sometimes you will need just to get the HTML output from a server control for a different usage like sending it by an e-mail. So what can you do? There is a method called RenderControl, which renders a control using a HtmlTextWriter object. We can simply load the control using the LoadControl method and then use RenderControl.
Control myControl = LoadControl("~/MyControl.ascx"); StringBuilder sb = new StringBuilder(); using (StringWriter sw = new StringWriter(sb)) { using (HtmlTextWriter tw = new HtmlTextWriter(sw)) { myControl.RenderControl(tw); } } Console.WriteLine(sb);
But the output is not what we really want. It contains only the static HTML tags. The server controls inside our control are not rendered. Why? Because in our way, the control does not follow the page life cycle. So, what we should do is to add it to a page.
Page page = new Page(); Control myControl = LoadControl("~/MyControl.ascx"); StringBuilder sb = new StringBuilder(); page.Controls.Add(myControl); using (StringWriter sw = new StringWriter(sb)) { Server.Execute(page, sw, false); } Console.WriteLine(sb);
Server.Execute method executes an IHttpHandler object and writes the output using a TextWriter object. In this way we follow the whole page life cycle so our control will render everything inside it.
Filled under ASP.NET. No Comments.

