Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a legacy ASP.NET ASCX User Control in MVC Razor view

I am trying to implement a MVC Razor _Layout.cshtml page that uses a WebForm ascx User Control (non-MVC). I am doing this based off the "Yes" section of this Scott Hansleman article "Mixing Razor Views and WebForms Master Pages with ASP.NET MVC 3" http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx

The article says how to use the same ascx User Control in both a webform Site.Master page as well as an MVC Razor _Layout page.

From what I've read elsewhere on Stackoverflow it is possible to use legacy ascx user controls (as well as ASP.NET webform server controls) in MVC pages. Using the following line should render the ascx user control in my Razor _Layout:

@{ Html.RenderPartial("~/UserControls/WebUserControl1.ascx"); }

However, that throws the error:

The view at '~/UserControls/WebUserControl1.ascx' must derive from ViewPage,
ViewPage<TModel>, ViewUserControl, or ViewUserControl<TModel>.

I have also tried the below with similar results:

@Html.Partial("~/UserControls/WebUserControl1.ascx")

What am I missing here?

like image 605
Walter Avatar asked Oct 28 '11 20:10

Walter


3 Answers

As the error message states, the user control "must derive from ... ViewUserControl". In the code behind file for your user control, simply change this...

public partial class WebUserControl1 : UserControl
{
    // ...
}

... to this:

public partial class WebUserControl1 : ViewUserControl
{
    // ...
}

ViewUserControl inherits from UserControl, so it will continue to work in your existing WebForms pages.

You may have to deal with additional issues beyond this one in order to get your user control to work in MVC, though. At least one other that I encountered is (alluded to by SLaks):

Control 'controlId' of type 'controlType' must be placed inside a form tag with runat=server.

If you encounter stuff like this, you're going to have to get creative. Either modify the user control so that it can live happily in both WebForms and MVC (replace the offending controls with generic HTML equivalents - with all the implications of that), or duplicate it so you have a WebForms version and an MVC version.

For example, you will have to replace things like <asp:TextBox ID="search"> with <input type="text" name="search" />, which means you also need to modify the server-side code that handles the value coming from that input. Basically you have to neuter the user control, converting it from something that used to embody both view rendering logic and post-back handling logic, into something that just renders a view.

You can make one ascx control play nicely with both WebForms and Razor MVC, and for simple things like page headers and footers or read-only views of data this is a good approach for migrating an application to MVC. But for more complex things like input forms, it will probably be easier to maintain both an ascx user control and an MVC Razor view.

like image 166
Daniel Schilling Avatar answered Oct 20 '22 14:10

Daniel Schilling


you can use asp.net user control in MVC by the following way

1st change user control inheritance this as by default

default:

public partial class TestControl: UserControl
{
// ...
}

change:

public partial class TestControl: ViewUserControl
{
// ...
}`

2nd user control with asp.net controls like this one within form tag and script manager should be call

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="TestApp.UserControls.TestControl" %>

<div>
<h1>Welcome Test User Control</h1>
<form id="test" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:TextBox runat="server" ID="txtbxTest" Text="Hello">

</asp:TextBox>
</form>
</div>

3rd Call user control in MVC View as

@{ Html.RenderPartial("~/UserControls/TestControl.ascx"); }

it should work without any issue, and it will be beneficial to convert old asp.net website into MVC, if old website have user controls, its so easy to upgrade with new technology in MVC.

like image 35
adnan Avatar answered Oct 20 '22 15:10

adnan


You cannot use classic ASP.Net usercontrols (w/ postbacks and ViewState) in ASP.Net MVC.

Html.Partial only allows you to render partial views, which happen to use the same extension, but are not the same.

like image 3
SLaks Avatar answered Oct 20 '22 16:10

SLaks