6

I have a website in asp.net 4.0 with 10 pages and 12 user controls.

Now i want to migrate this site in asp.net MVC 4.0; So can I re-use those user controls (that are there in traditional asp.net) and render them using <%= Html.RenderUserControl() %>.

In short my question is whether we can use asp.net's user control in asp.mvc 4?

Many thanks,

JIgar

6
  • It depends. If they don't depend on ViewState, it might work. All comes down to what's in the control. Commented Mar 15, 2012 at 13:53
  • Craig stuntz :- Can you lead me to some profound links? better answers? Commented Mar 15, 2012 at 13:54
  • 1
    see this :stackoverflow.com/questions/6317317/… Commented Mar 15, 2012 at 13:58
  • I'm not sure I know a better answer than, "Try it and see if it works." I've made this work for DayPilot, but it fails for the SSRS viewer. Commented Mar 15, 2012 at 13:59
  • @Jigs he doesn't seem to be using Razor. Commented Mar 15, 2012 at 14:00

2 Answers 2

5

You generally can't use asp.net controls in a MVC application. Almost all controls require view state, which ASP.NET MVC doesn't support.

Sign up to request clarification or add additional context in comments.

1 Comment

You may be able to use some trickery to half have them work, but web forms controls require things like view state, and the page life cycle (init, load, ... prerender) and mvc is totally different. We encountered the same problem where we had a user control that we'd like to re-use in mvc and the short answer was essentially that we needed to rebuild it using razor views and models. Perhaps someone could build a web forms component to somehow include in a mvc page but I don't think there is such a thing at present.
0

Nice Question Jigar Pandya;

Yes; we can use ASP.NET WebForm User Controls in MVC Site.

One way we would be able to use user controls in ASP.NET MVC is to add a new item of type webform.

Then on that webform we would be able to use ASP.NET User Control's in a MVC project. I had the same question since we had many existing user controls we wanted to use. Hope that helps.

6 Comments

I can't see a way to add a "new item of type webform". I think you need to explain that part. The answer to the question seems to be that this is not possible.
In solution explorer right click "add new item" then under installed templates choose "web form" msdn.microsoft.com/en-us/library/0hds5k93.aspx Hope that helps you Action Dan
Hi that referenced page is titled "How to: Add ASP.NET Web Forms Page to a Web Project". The question is about adding a web forms user control to a mvc project. Something which apparently is not possible in the way some would hope.
When I read in your first comment you were trying to add a "new item of type webform" that is what I was linking about & in the answer I stated Yes; we can use ASP.NET WebForm User Controls in MVC Site. The UC can be on the webform page. Hopefully you can see how to add that as well. Hope that helps!
Agree Action Dan; we can add a UC to an MVC project but we cannot add a UC to a MVC razor view.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.