<p>Like many others on this site I am considering a move to ASP.NET MVC for future projects.  Currently my sites are running the traditional ASP.NET 2.0 Web Forms, and it works OK for us, so my other option is just to stick with what I know and make the move to ASP.NET 3.5 with the integrated AJAX stuff.</p>

<p>I'm wondering about how user controls work in ASP.NET MVC. We have tons of <code>.ASCX</code> controls, and a few composite controls.  When I work with web designers it is very easy to get them to use ASCX controls effectively, even without any programming knowledge, so that's a definite plus.  But then of course the downsides are the page life cycle, which can be maddening, and the fact that ASCX controls are hard to share between different projects.  Composite controls are share-able, but basically a black box to a designer.</p>

<p>What's the model in ASP.NET MVC?  Is there a way to create controls that solves the problems we've dealt with using ASCX and composite controls?  Allowing easy access for web designers without having to worry about code being broken is an important consideration.</p>

[{'answer_id': 41722, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 4, 'selected': True, 'text': '<p>To implement a user control you do the following call:</p>\n\n<pre><code>&lt;% Html.RenderPartial("~/Views/Shared/MyControl.ascx", {data model object}) %&gt;\n</code></pre>\n\n<p>You may also see the older syntax which as of PR5 is not valid anymore</p>\n\n<pre><code>&lt;%= Html.RenderUserControl("~/Views/Shared/MyControl.ascx", {data model object}) %&gt;\n</code></pre>\n\n<p>You will always have to worry about code breaking when moving from Web Forms to MVC, however the ASP.NET MVC team has done a great job to minimize the problems.</p>\n'}, {'answer_id': 41741, 'author': 'LorenzCK', 'author_id': 3118, 'author_profile': 'https://Stackoverflow.com/users/3118', 'pm_score': 1, 'selected': False, 'text': '<p>As Nick suggested, you will indeed be able to render your user controls, but obviously the page-cycle, pagestate and postback from traditional ASP Webforms won\'t work anymore, thus making your controls most likely useless.</p>\n\n<p>I think you\'ll have to rewrite most of your complex controls to port your website to MVC, while simple controls which, for instance, provide only formatting and have no postback status, should simply work.\nThe code provided by Nick will simply work in this case.</p>\n\n<p>And about sharing between more projects: I think controls will be more like "reusable HTML-rendering components" that can be shared across a website, rather than "reusable code components" with logic (like WebForms controls). Your web logic will/should be in the pages controllers and not in the HTML controls. Therefore sharing controls across more projects won\'t be so useful as in the WebForms case.</p>\n'}, {'answer_id': 45091, 'author': 'Matt Hinze', 'author_id': 2676, 'author_profile': 'https://Stackoverflow.com/users/2676', 'pm_score': 1, 'selected': False, 'text': '<p>Yeah, you can do RenderPartial. That\'s a good start. But eventually these guys will need logic and other controller type stuff. Be on the lookout for a subcontroller implementation from the framework team.  There should also be something in MvcContrib soon. Or roll your own.</p>\n\n<p><strong>Edit:</strong> I just posted about this here: <a href="http://mhinze.com/subcontrollers-in-aspnet-mvc/" rel="nofollow noreferrer">http://mhinze.com/subcontrollers-in-aspnet-mvc/</a></p>\n'}, {'answer_id': 5192101, 'author': 'Trevor', 'author_id': 644471, 'author_profile': 'https://Stackoverflow.com/users/644471', 'pm_score': 1, 'selected': False, 'text': "<p>MVC has different page life cycle compare to your user control. </p>\n\n<p>You may consider this to re-write.</p>\n\n<p>The aspx is the <em>view</em>. You still need a re-write, the syntax is different.\nJavaScript will work. But I hardly find the WebControls will work. Because MVC does not have viewstate and postback anymore.</p>\n\n<p>For the code behind (aspx.cs) you need to convert that to be a <em>Controller</em> class.\n<code>Page_Load</code> method will no longer works. You probable leave it to <code>Index()</code> method.</p>\n\n<p><em>Model</em> is simply the entity classes that your code behind consume. </p>\n\n<p>Conclusion, it's a total rewrite. Cheers. Happy coding.</p>\n"}]
['https://Stackoverflow.com/questions/41717', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1219/']