19

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?

JustLearning
  • 2,644
  • 2
  • 27
  • 48
balexandre
  • 69,002
  • 44
  • 219
  • 321

1 Answers1

21

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>
codingbiz
  • 25,120
  • 8
  • 53
  • 93