19

我正在接触一个旧的WebForms project,但我早就离开了它,我现在已经习惯了 MVC。我正在尝试重构项目并提出一个让我发疯的简单问题......

.aspx文件包含在另一个文件中的最佳方式是什么?

我不想仅仅为此而拥有很多主文件,我所追求的只是某种东西@Html.RenderPartial("MyFileName")

在某些现有文件中包含文件有那么难吗?

4

1 回答 1

21

在 UserControl 上使用UserControl 教程。它们是带有.ascx扩展名的文件,您可以将它们包含在您的页面中

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

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

将其包含在您的.aspx页面中

<%@ 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>
于 2012-10-06T15:31:47.173 回答