C# 将css类添加到body标记c的反射#
我有一个母版页,在body标签上设置了runat=“server”&Id。见下文C# 将css类添加到body标记c的反射#,c#,reflection,C#,Reflection,我有一个母版页,在body标签上设置了runat=“server”&Id。见下文 <body id="MasterPageBodyTag" runat="server"> 现在我想将css类添加到App_code文件夹中Class1.cs文件的body标记中 在.aspx am上,使用以下代码传递母版页控件: public HtmlGenericControl BodyTag { get { return MasterPageBodyTag; } set
<body id="MasterPageBodyTag" runat="server">
现在我想将css类添加到App_code文件夹中Class1.cs文件的body标记中
在.aspx am上,使用以下代码传递母版页控件:
public HtmlGenericControl BodyTag
{
get { return MasterPageBodyTag; }
set { MasterPageBodyTag = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
backend.FindPage((PageTemp)this.Master);
}
现在在Class1.cs上,我有以下内容
public static void FindPage(Control mp)
{
Page pg = (Page)HttpContext.Current.Handler;
PropertyInfo inf = mp.GetType().GetProperty("BodyTag");
}
我想在BodyTag中添加以下内容
// BodyTag.Attributes.Add("class", "NewStyle");
但似乎找不到一种方法来添加属性或将inf强制转换到HtmlGenericControl
任何帮助都会很好。您正在做的事情似乎有点复杂,可能有更简单的方法来处理 要回答您的问题,您不需要使用反射。您只需将要传递给
FindPage
的参数强制转换为已创建的母版页类型
您尚未指定母版页的类型名称,因此我将为其命名为MyMasterPage
所以FindPage
应该是这样的:
public static void FindPage(Control mp)
{
var masterPage = mp as MyMasterPage;
if (mp != null)
{
mp.BodyTag.Attributes.Add("class", "NewStyle");
}
}
您需要在aspx文件上添加以下内容:
<%@ MasterType VirtualPath="~/Your_Master_Page.Master" %>
与其依赖于母版页类型,我只需使用FindControl按Id搜索body元素。假设body标记位于顶级母版页上,还假设您可能使用嵌套母版页,它看起来像:
private static MasterPage GetTopLevelMasterPage(Page page)
{
MasterPage result = page.Master;
if (page.Master == null) return null;
while(result.Master != null)
{
result = result.Master;
}
return result;
}
private static HtmlGenericControl FindBody(Page page)
{
MasterPage masterPage = GetTopLevelMasterPage(page);
if (masterPage == null) return null;
return masterPage.FindControl("MasterPageBodyTag") as HtmlGenericControl;
}
private void UpdateBodyCss()
{
HtmlGenericControl body = FindBody(this);
if(body != null) body.Attributes.Add(...);
}
您甚至可以通过搜索标记名为“body”的HtmlGeneric
控件来删除对id的依赖:
你不能用jQuery或类似的工具在客户端完成吗?这有用吗?我会将FindControl与Id一起使用,而不是依赖母版页类型。这样做会导致以下错误:对象引用未设置为对象的实例。@Joe:更好的主意:-)@Chris:您得到的异常提示
MasterPageBodyTag
为空。检查设置是否正确,或者最好使用Joe的建议!这对我有用。我尝试使用解决方案@,但无法使其发挥作用。我使用了该解决方案并对标记进行了处理,但是什么原因导致ContentPlaceHolder的控件和表单甚至在页面源代码中也无法加载?更具体地说,我实际上在一个名为_Culture的类中使用了这个解决方案,该类带有Inherits System.Web.UI.Page,然后我在aspx页面中调用它,就像Public类_defaultinherits _CultureOh,我知道了。。抱歉,这是因为我已将更新代码添加到受保护的覆盖子InitializeCulture()而不是页面加载
private static MasterPage GetTopLevelMasterPage(Page page)
{
MasterPage result = page.Master;
if (page.Master == null) return null;
while(result.Master != null)
{
result = result.Master;
}
return result;
}
private static HtmlGenericControl FindBody(Page page)
{
MasterPage masterPage = GetTopLevelMasterPage(page);
if (masterPage == null) return null;
return masterPage.FindControl("MasterPageBodyTag") as HtmlGenericControl;
}
private void UpdateBodyCss()
{
HtmlGenericControl body = FindBody(this);
if(body != null) body.Attributes.Add(...);
}
private static HtmlGenericControl FindBody(Page page)
{
MasterPage masterPage = GetTopLevelMasterPage(page);
if (masterPage == null) return null;
foreach(Control c in masterPage.Controls)
{
HtmlGenericControl g = c as HtmlGenericControl;
if (g == null) continue;
if (g.TagName.Equals("body", StringComparison.OrdinalIgnoreCase)) return g;
}
return null;
}