Are you looking for an affordable hosting service? Are you looking for good service at affordable prices? Try HostForLife.eu, only with € 3.00/month, you can get a reasonable price with best service. This topic contains only brief information about ASP.NET MVC, if you want to be more familiar with ASP.NET MVC, you should try HostForLife.eu.

Extending HtmlHelper Class Using Extension Method:


Creating ASP.NET MVC Helper is not rocket science and can be performed just by extending the HtmlHelper class using extension methods. Inside the extension method you can return the HTML of the control you are trying to build. The View will consume the HTML and display the control. The main part of this process is to generate the HTML for the control. Let's take a look at different ways you can accomplish it.

Building HTML String Manually:

The easiest and the simplest way to create a new HTML helper is to return a string from the extension method. The code below shows how to implement a simple HTML helper to create a customized Blue TextBox control.

public static string BlueTextBox(this HtmlHelper helper)
      {
          return “<input type=’text’ style =’background-color:blue’/>”;
      }

In the above code we are simply creating the HTML for the control by returning the string back to the view. The view will use the BlueTextBox as shown below:

<%= Html.BlueTextBox() %>

As, expected the HTML generated by the BlueTextBox will be the same HTML that you have returned from the BlueTextBox extension method as shown below:

<input type=’text’ style=’background-color:blue’/>

This was just one of the ways to develop your HTML helpers. In the next section we will demonstrate how you can use the TagBuilder class to create HTML helpers.

Using TagBuilder Class to Construct HTML:

The TagBuilder class is part of the System.Web.Mvc assembly and it allows you to create HTML tags. The purpose of the TagBuilder class is to provide extra assistence when developing HTML controls. It removes the string concatenation problems from the equation and provide an easy way to construct HTML. The code below shows how to use the TagBuilder class to construct the BlueTextBox control.

public static string BlueTextBox(this HtmlHelper helper)
       {
           var tagBuilder = new TagBuilder(“input”);
           tagBuilder.Attributes[“style”] = “background-color:blue”;
           return tagBuilder.ToString();
       }

Since, the input type HTML elements can be of different type namely radio, checkbox, textbox, password etc. You can specify the "type" attribute to represent the type of the control you want to create.

public static string BlueTextBox(this HtmlHelper helper)
      {
          var tagBuilder = new TagBuilder(“input”);
          tagBuilder.Attributes[“style”] = “background-color:blue”;
          tagBuilder.Attributes[“style”] = “radio”;
          return tagBuilder.ToString();
      }

The above code will produce the following HTML:

<input style=”background-color:blue” type=”radio”></input>

The next method uses the Webforms control to render the HTML to the client.

Using Webforms ASP.NET Controls:

You can always use Webform controls to render the HTML of the control to a TextWriter and return the generated HTML to the view. The implementation is shown below:    

public static string BlueTextBox(this HtmlHelper helper)
       {
          var sb = new StringBuilder();
          var sw = new StringWriter(sb);
          var htw = new HtmlTextWriter(sw);

          var tb = new TextBox();
          tb.BackColor = System.Drawing.Color.Blue;

          tb.RenderControl(htw);

          return sb.ToString();
       }

If you are not looking for much customization then you can use the above method and use the already existing Webforms controls in your ASP.NET MVC application.

Conclusion:

HTML helpers are easy to create and there are multiple ways to implement them. Depending on the scenario you can choose any of the above methods to create your Html helpers. In the next article we will implement the SelectList HTML helper which can be used to create RadioButtonList, CheckBoxList etc.

Top Reasons to host your ASP.NET MVC Website with HostForLife.eu

There are many reasons why so many people choose HostForLife over any other web hosting provider each year. Whether you’re beginner or an experience webmaster, HostForLife offers the perfect solution for everyone.

You’ll have highly trained, skilled professional technical support people ready, willing, and wanting to help you 24 hours a day. Your web hosting account servers are monitored from three monitoring points, with two alert points, every minute, 24 hours a day, 7 days a week, 365 days a year. The followings are the list of other added-benefits you can find when hosting with us:

1. World-class 24x7 Customer Support
Will your hosting company promptly answer questions and resolve issues - at 3 am on a Sunday? Even some providers claiming “24x7” support will not - but HostForLife will. Our outstanding uptime is backed by true 24x7 customer support. An expertly trained technician will respond to your query within one hour, round the clock. You will also get qualified answers. Other hosting companies typically have very low - level support staff during the night or weekends. HostForLife always has knowledgeable, top - level  support standing by, day or night, to give you the answers you need.

2. Commitment to Outstanding Reliability
Reliability, Stability, and Performance of our servers remain out TOP priority. Even our basic service plans are equipped with standard service level agreements for 99.99% uptime. Advanced options raise the bar to 99.99%. Our state-of-the-art data centers combine servers and SAN storage with full redundancy and operational tools with proprietary service management techniques. Full backup and recovery capabilities are implemented, including redundant power supplies, cooling and connectionsto major data networks.

3. “Right-size” plans for maximum value
HostForLife offers a complete menu of services. IT professionals select only what they need - and leave behind what they don’t. The result is an optimal blend of cost and performance. We offer IT professionals more advanced features and the latest technology - ahead of other hosting companies.

4. Profitable, Stable, Debt-free Business
Financial stability is the bedrock of a hosting provider’s ability to deliver outstanding uptime, cost-effective service plans and world-class 24x7 support.  HostForLife’s customers are assured of our financial integrity and stability - a stark contrast to the ups and downs they may have experienced with other providers.

5. The Best Account Management Tools
HostForLife revolutionized hosting with Plesk Control Panel, a Web-based interfaces that provides customers with 24x7 access to their server and site configuration tools. Some other hosting providers manually execute configuration requests, which can take days. Plesk completes requests in second. It is included free with each hosting account. Renowned for its comprehensive functionally - beyond other hosting control panels - and ease of use, Plesk Control Panel is available only to HostForLife’s customers.

6. 30-Day Money Back Guarantee
HostForLife 30 day money back guarantee ensures you have the ability to cancel your account anytime within your first 30 days under our full 30 day money back guarantee (less one-time account setup free). So what are you waiting for? Sign up today, risk free…

7. Simplicity with FREE 1-Click Installation
HostForLife was designed with ease of use in mind. From one click installations of your favourite website applications to our much talked about drag and drop website builder, you can rest assure your stay with us is going to be a smooth one. HostForLife offers the most extensive set of scripts on the web allowing you to build complicated websites with little or no programming knowledge at all. From blogs to forums to powerful e-commerce solutions, Super Green has something that is right for you.