gpt4 book ai didi

asp.net - 无法将母版页绑定(bind)到我的页面

转载 作者:行者123 更新时间:2023-12-01 04:38:11 25 4
gpt4 key购买 nike

我是 ASP.Net 的新手,我想创建一个非常简单的表单来绑定(bind)到母版页,但没有成功。这是我的母版页代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>
<body>
<form runat=server>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
<body>
<table width=100% border=1 style="background:black" >
<tr>
<td width=20%></td>
<td width=80%><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
<td width=20%></td>
</tr>
</table>
</body>
</asp:ContentPlaceHolder>
</form>

</body>
</html>

我页面的代码是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<asp:Content ContentPlaceHolderID=HeadContent runat=server>
</asp:Content>

<asp:Content ID="MainContent" runat=server ContentPlaceHolderID=MainContent >
</asp:Content>

但是母版页中的表格没有出现在页面中,请问您能帮我解决这个问题吗?

最佳答案

首先,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<asp:Content ContentPlaceHolderID=HeadContent runat=server>
</asp:Content>

<asp:Content ID="MainContent" runat=server ContentPlaceHolderID=MainContent >
</asp:Content>

完全错误。

DOCTYPE 应该在母版中列出。其次:

<asp:Content ID="HContent" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>

<asp:Content ID="MainContent" runat="server" ContentPlaceHolderID="MainContent" >
</asp:Content>

会更符合您的意图。但是你的风格有一个缺陷,可以说,这完全是一种风格:

<asp:Content ID="HContent" ContentPlaceHolderID="HeadContent" runat="server" >
</asp:Content>

<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server" >
</asp:Content>

注意我是如何将 runat="server" 放在两个标签的末尾的吗?始终将其放在最后,这样可以更轻松地确保您将其放在正确的位置。或者,始终将其放在 ID

之前

编辑:

<asp:ContentPlaceHolder ID="MainContent" runat="server">
<body>
<table width=100% border=1 style="background:black" >
<tr>
<td width=20%></td>
<td width=80%><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
<td width=20%></td>
</tr>
</table>
</body>
</asp:ContentPlaceHolder>

在母版页中有此内容的地方,当子页实现此部分时,ContentPlaceHolder 的内容将不会显示。您需要将它们移到母版页 ContentPlaceHolder 声明之外。这个概念非常“非此即彼”,没有其他意见的余地。特意这样写的。

也许你想要:

<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<body>
<table width=100% border=1 style="background:black" >
<tr>
<td width=20%></td>
<td width=80%><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
<td width=20%></td>
</tr>
</table>
</body>

关于asp.net - 无法将母版页绑定(bind)到我的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11024607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com