在ASP.NET Web 表单中制作部分页面

Make partial page in ASP.NET Web form

我正在处理 ASP.NET Web 表单应用程序。 我正在设计包含大量 html 元素的页面。 我想将这些元素分开并制作部分页面,然后将它们包含在一页中以提高代码可读性和易于维护。 我可以在 razor 中执行此操作,但我找不到有关此 Web 表单的任何教程。

有人可以帮忙吗?

使用用户控件Tutorial on 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>

更多详情请看这里:

Creating a UserControl

Using a UserControl

对于Web Form,您可以创建user control来创建部分页面。 您可以在此 MSDN URL 上阅读有关它们的信息:ASP.Net User Controls

用户控件的命名总是以 ascx 结尾,例如 EditProduct.ascxRegisteration.ascx。如果您想在 Web Form aspx page 中使用它们,那么您可以简单地 drag-n-drop 来自 Solution Explorer 中的用户控件 Visual Studio IDE是的,用户控件提升了 re-use 和更易于维护。