Is there a way to render Partial Views in WebForms?

balexandre picture balexandre · Oct 6, 2012 · Viewed 10.1k times · Source

I'm touching an old WebForms project but I'm long gone from it and I'm now to used to MVC. I'm trying to refractor the project and come up with a simple issue that is making me crazy...

What would be the best way to include an .aspx file within another?

I don't want to end up having a lot of Master Files just for this, all I'm after is something like @Html.RenderPartial("MyFileName") kind'a thingy or

it's that hard to include a file in some existing files?

Answer

codingbiz picture codingbiz · Oct 6, 2012

Use UserControl Tutorial on UserControl. They are files with .ascx extension and you can include them in your pages

//UserControl1.ascx
<% @ Control Language="C#" ClassName="UserControl1" %>

 <div>
   My Custom Control: I can use any Server controls, html tags etc
 </div>

Include it in your .aspx page

<%@ Page Language="C#" %>
<%@ Register TagPrefix="uc" TagName="MyCustomControl" Src="~/Controls/UserControl1.ascx" %>
<html>
<body>
<form runat="server">
    <uc:MyCustomControl id="MyPartialView" 
        runat="server" />
</form>
</body>