问题
Root.Master
<%@ Master Language="VB" CodeFile="~/Root.master.vb" Inherits="Root"%>
<div id="<%=MyPage%>">
<asp:ContentPlaceHolder ID="Content" RunAt="Server"/>
</div>
Root.Master.vb
Partial Class Root
Inherits BaseMaster
End Class
Page.vb (在App_Code文件夹中)
Public Class BaseMaster
Inherits System.Web.UI.MasterPage
Public MyPage As String
End Class
Index.aspx.vb
Partial Class Index
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
CType(Me.Master, BaseMaster).MyPage = "Page"
End Sub
End Class
答案 0 :(得分:1)
我认为当设置为runat =“server”时,您无法更改元素的ID。
为了为body标签提供多个ID,您必须为每个不同的ID拥有一个母版页,并通过代码选择母版页。
如果要为CSS目的更改其ID,请考虑使用类。
修改:如果您在body标签中不需要runat =“server”属性,则可以这样:
基本思想是在主页面中为动态ID设置一个变量,并能够从.aspx页面和.ascx控件设置其值。
变量BodyID在继承MasterPage类的类中声明,而您的母版页继承此类。
母版页标记
<body id="<%=BodyID%>">
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
后面的Maste页面代码
Public Class Site1
Inherits BaseMaster
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
BaseMaster类
Public Class BaseMaster
Inherits System.Web.UI.MasterPage
Public BodyID As String
End Class
如何在Web表单中使用它
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
CType(Me.Master, BaseMaster).BodyID = "body_dynamic_id"
End Sub