This website is no longer actively supported

Overview

Last week I wrote a wiki page MVC 3 Razor : Creating Custom HTML Helpers about extending the HTML Helpers to be able to render controls read-only. I also needed to extend the Telerik MVC controls to render the Telerik MVC controls read-only as well. I took a look at the Telerik.Web.Mvc.dll assembly using .NET Reflector and looked to see how Telerik extended the HTML Helper with their ViewComponentFactory, what we usually see as the Html.Telerik() method. The ViewComponentFactory creates an instance of a ViewComponentBuilder to "build" the control. The ViewComponentBuilder has a method called Render that renders the component as a control on the page.

Since I wanted to be able to render the Telerik controls as read-only, I overloaded the Render method to take a parameter of bool that indicates whether the control is editable or not.

Overloading the Render Method for the DropDownList Control

The class that derives from ViewComponentBuilder to render the DropDownList control is the DropDownListBuilder. If the DropDownList control is editable, the DropDownListBuilder.Render method is executed. If it is read-only, then the System.Web.Mvc.TagBuilder class is used to create a read-only input control. The DropDownListBuilder is used to get the Name and Value for the DropDownList control.

Loading a Partial View That Contains a Grid Using jQuery.ajax

I recently posted an articles about loading a partial view that contains a Telerik Grid and other Telerik Controls. I started thinking about wrapping the calls to Render(), WriteInitializationScript() and creating the hidden element in an extension method. Turns out that it was pretty easy to do. I extended the Builder class for each control by adding a RenderAjax method that encapsulates the Render() and WriteInitializationScript() methods as well as the creation of the hidden element with the jQuery call to create the jQuery objects for each control. I still needed to use eval to evaluate the string in the hidden field to execute the jQuery to create the jQuery object for the control, but I still haven't given up the idea of coming up with a solution for this!