Home > Programming > Using Web Forms user controls in an ASP.NET MVC project

Using Web Forms user controls in an ASP.NET MVC project

We have an existing ASP.NET Web Forms project that we’re in the process of slowly converting to ASP.NET MVC. We do not have the manpower to simply rewrite the entire website from scratch, so one of our goals is to slowly convert the website piece by piece. This means that the existing Web Forms project will need to coexist with emerging MVC project.

In the current Web Forms project, we have a number of user controls that are used on all our pages. In order to maintain backwards compatibility with our Web Forms pages, we couldn’t rewrite the controls as MVC helpers without having to duplicate code.

I created this helper that allows us to reuse Web Forms user controls on views:

public static class UserControlHelper
    public static HtmlString RenderControl<T>(this HtmlHelper helper, string path)
        where T : UserControl
        return RenderControl<T>(helper, path, null);
    public static HtmlString RenderControl<T>(this HtmlHelper helper, string path, Action<T> action)
        where T : UserControl
        Page page = new Page();
        T control = (T)page.LoadControl(path);
        if (action != null)
        using (StringWriter sw = new StringWriter())
            HttpContext.Current.Server.Execute(page, sw, false);               
            return new HtmlString(sw.ToString());

By instantiating a Page and adding the user control to it, we can render the result as a string. To use the helper on a view, we can call it like this:


The entire method call needs to be surrounded with parentheses, otherwise the angle brackets will confuse the Razor view engine. If the user control has properties that need to be set, we can call the method overload and pass it an expression:

@(Html.RenderControl<MyCustomControl>("~/Test/MyCustomControl.ascx", x => x.Name = "Malvin"))
@(Html.RenderControl<MyCustomControl>("~/Test/MyCustomControl.ascx", x =>
        x.Name = "Malvin";
        x.Age = 25;
  1. Kiran
    June 30, 2011 at 1:34 pm

    This seem to be working only if the User Contorl as read-only data. If there are any buttons and if there are any server events on the button clicks, the events are not getting fired. Probably, the event wiring with server controls is missing. Is there a way to solve this issue quickly?

    • Raj
      August 30, 2011 at 9:32 pm

      I am also interested if there is a solution for wiring up the events of asp.net usercontrols?

    • RazorBlade
      December 30, 2012 at 4:47 pm

      RE: This seems to be working only if the User Contrl as read-only data…
      Indeed the approach seems appealing at first sight… but to good to be true? I’m afraid it ends right there as far as options… the control will be rendered in read-only . So if you try to create, let’s say an ascx control
      which contains an asp:GridView binded to a database query, the result will be displayed all right in the gridview, but I’m afraid you won’t be able to do anything else afterwards, like clicking on checkbox in a cell, not even adding sorting, nor paging… it seems pretty much static at this stage… too bad…

  2. Dave
    September 8, 2011 at 4:30 pm

    Yes, I am interested in using this on controls that do not just render read only data. Thanks

  3. Thiago Passos
    December 20, 2012 at 5:38 am

    Awesome man! Exactly what I needed. migrating old projects to MVC has been easy after this html helper. Cheers!

  4. Glocken
    July 20, 2013 at 9:17 am

    I’m interested in reusing my ascx controls as well. and I’ve searched a lot but there is no solution that can make ascx fully reusable in mvc (that doesn’t make sense though, I just want it for migrating proposes). It seems that the only approach for doing that is to make user controls, postback free. after that may be we can use this helper to load them.
    any idea to eliminate postbacks?

  5. Srilaxmi
    February 3, 2014 at 2:37 am

    It seems good , but button events are not fired and hidden field values are not getting , please help me its very urgent

  6. Srilaxmi
    February 3, 2014 at 2:44 am

    Srilaxmi :
    It seems good , but button events are not fired and hidden field values are not getting in usercontrol, please help me its very urgent

  7. October 1, 2014 at 7:37 pm

    great issues altogether, you simply received a new reader.
    What would you recommend in regards to your submit that you simply made
    some days ago? Any certain?

  8. October 28, 2014 at 12:10 am

    Excellent blog here! after reading, i decide to buy a sleeping bag ASAP

  9. April 18, 2015 at 9:45 am

    Thanks for the marvelous posting! I quite enjoyed reading it, you could be a great author.I will be sure to bookmark your blog and definitely will come back
    sometime soon. I want to encourage that you continue your great
    writing, have a nice evening!

  10. September 29, 2016 at 10:37 pm

    If you had the worst procurement experience online with a company that you truly thought you could trust, will you still go for another round of orders through it? Well, I’m actually talking about A.C.C. Inc., also known as AdvanTec Computer Company.

  11. November 17, 2016 at 6:33 am

    Is this how other libraries, like Shield UI for ASP.NET MVC (http://demos.shieldui.com/mvc/grid-general/basic-usage) were implemented?

  12. April 14, 2019 at 8:02 pm

    I do not know whether it’s just me or if everyone else encountering
    issues with your blog. It appears like some of the written text in your posts are running off
    the screen. Can someone else please comment and let me know if this
    is happening to them too? This might be a problem with my
    web browser because I’ve had this happen previously.


  13. April 23, 2019 at 1:09 pm

    I am genuinely thankful to the owner of this
    web site who has shared this enormous article
    at at this time.

  14. May 4, 2019 at 1:34 pm

    It iѕ nott my first time to pay a quick visit this web page, і
    am browsіng this web page daiⅼly and obtain nice facts from heгe every
    day. http://donohoememorials.com/

  15. May 11, 2019 at 2:57 pm

    When I initially commented I seem to have clicked the -Notify me when new comments are added- checkbox and
    from now on whenever a comment is added I recieve four emails
    with the exact same comment. Is there a way you are able to remove me from that service?
    Appreciate it!

  16. May 13, 2019 at 10:14 pm

    An impreѕsive share! I’ve just forwarded this onto a friend who hɑd been doking a little һomework on this.
    Annd he in fact ordered me bгeakfast simply because I dіscoverеd it for
    him… lol. So let me reword this…. Thanks for
    thee meal!! But yeah, thanks for spending time to talk about this
    topic here on yojг blog. http://timewarp-toys.com

  17. May 21, 2019 at 7:19 am

    Hi, I want to subscribe for this weblog to get latest updates, so where
    can i do it please help out.

  18. July 11, 2019 at 7:20 am

    I go to see day-to-day a few web pages and websites to read posts, however this web site
    presents feature based writing.

  1. August 6, 2015 at 1:48 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: