2

我正在尝试更改运行时 site.master 页面上存在的 css 类属性,但到目前为止我已经厌倦了

mainContainer.Attributes.Add("style", "background-image('myImage.png')");

mainContainer.Attributes.Add("class", "className");

但是这些都不能让我在运行时更改主文件的 css。我正在使用 c# 使用 asp.net

这是母版页上的代码

 <div class="main">

            <asp:ContentPlaceHolder ID="MainContent" runat="server"/>


        </div>
4

3 回答 3

1

您需要在其他子页面中部分加载母版页,如下所示...

<%@ MasterType VirtualPath="~/Site1.Master" %>

然后在子页面的页面加载中..put

protected void Page_Load(object sender, EventArgs e)
    {
        HyperLink contact_menu = (HyperLink)Master.FindControl("contactmenu");
        contact_menu.CssClass = "current";
    }

根据您的需要更改..享受..

于 2012-10-26T10:50:06.707 回答
1

ContentPlaceHolder是输出 html 代码中不存在的元素。它只定义了一个区域。您可以尝试使用“main”类更改 div。只需添加 runat="server" 和 id 属性并从代码中访问。

<div id="MainDiv" class="main" runat="server">

接着

MainDiv.Attributes.Add...
于 2012-10-26T10:50:38.587 回答
0

您首先需要在母版页上找到控件

Image img = Page.Master.FindControl( "layoutStyleSheet" ) as Image;

然后为其添加样式

img.Attributes.Add("class", "className");
于 2012-10-26T10:51:26.840 回答