ASP.NET MVC Framework & Web UI Controls

Few days back ASP.NET MVC Framework was released. Since, the framework is new there are not many UI controls available yet! But off course this is all going to change. Actually this has already started to change since Rob Conery created the MVC UI Helper Controls. Scott Hanselman used some of Rob's cool controls in his demo. The purpose of the UI helper controls is to help the developer to quickly create UI controls. So, instead of using the foreach loop to populate a DropDownList you can use the UI helper controls to populate it using a single line of code.

I have not looked at Rob UI Control Library (I am sure it is awesome!) but have made few of my own helper methods. For example to create a TextBox control I can use the following code:

<%= Html.TextBox("mytextBox", "Hello World") %>  

I am extending the Html class and adding my own extension methods (just like Rob did!). My TextBox method looks something like this:

  public static string TextBox(this HtmlHelper helper,string id,string value)
        {
            TextBox t = new TextBox();
            t.ID = id;
            t.Text = value;
            return RenderControlToHtml(t);     
        }

If I want to add a button and attach onmouseover and onmouseout attributes to the button I will use the following code:

  <%= Html.Button("Btn_Submit","Submit","foo()",new ButtonAttribute().Add("onmouseover","this.style.backgroundColor='Yellow';")
         .Add("onmouseout","this.style.backgroundColor = 'Blue';"))%>  

And here is the Button method:

 public static string Button(this HtmlHelper helper, string id, string value, string functionName,
           ButtonAttribute attributes)
       {
           Button button = new Button()
           {
               ID = id,
               Text = value,
               OnClientClick = functionName   
              
           };

           foreach (var key in attributes.Attributes.Keys)
           {
               button.Attributes.Add(key, attributes.Attributes[key]);
           }

           return RenderControlToHtml(button);       

       }

Anyway, I am sure there are lot of MVC controls still to come and it will be very interesting to see how they compete with the ASP.NET server controls.

Print | posted @ Thursday, December 13, 2007 9:37 AM

Twitter