c# - 当页面在单击按钮时加载动态内容时,页脚 CSS 不起作用

标签 c# dynamic css

.html

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="dark.css" rel="stylesheet" type="text/css" id="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <asp:Label ID="CaptionLabel" runat="server"></asp:Label>
    <asp:TextBox ID="NumberTextbox" runat="server">(empty)</asp:TextBox>
    <asp:Button ID="SquareButton" runat="server" Text="Square" style="background-color:Blue; color:White;" />
    <asp:Label ID="ResultLabel" runat="server" Text="(empty)" CssClass="reverse"></asp:Label>
    <p>
        <asp:Label ID="Label1" runat="server" CssClass="footer1" 
            Text="Label Label Label Label LabelLabelLabel Label Label Label Label Label Label Label"></asp:Label>
    </p>
    <asp:RadioButton ID="radioDark" runat="server" AutoPostBack="True" 
        Checked="True" GroupName="grpSelectStylesheet" 
        oncheckedchanged="SwitchStylesheets" Text="Dark" />
    <br />
    <asp:RadioButton ID="radioLight" runat="server" AutoPostBack="True" 
        GroupName="grpSelectStylesheet" oncheckedchanged="SwitchStylesheets" 
        Text="Light" />
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
    </body>
</html>

.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void SwitchStylesheets(object sender, EventArgs e)
    {
        if (radioDark.Checked)
            stylesheet.Href = "dark.css";
        if (radioLight.Checked)
            stylesheet.Href = "light.css";

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        int count=DateTime.Now.Second;
        for (int i = 0; i < count; i++)
        {//for
            Label q = new Label();
            q.ID = DateTime.Now.Second.ToString();

            q.Text = DateTime.Now.Second.ToString();
            string spacee = "<br />";
            Label space = new Label();
            space.Text = spacee;
            form1.Controls.Add(q);
            form1.Controls.Add(space);
        }//for
    }
}

单击按钮时,它会正常工作,但页脚不会注册页面的扩展。

最佳答案

您的代码完全错误。即使自动回发设置为 true,也不能像这样更改控件的样式表。
更新:这是你应该如何做的:

1- 从您的页面中删除 .css 引用。
2- 将此方法添加到您的页面:

  private void UpdateStylesheet(string filepath)  
  {  
     HtmlLink newStyleSheet = new HtmlLink();  
     newStyleSheet.Href = filepath;       
     newStyleSheet.Attributes.Add("type", "text/css");  
     newStyleSheet.Attributes.Add("rel", "stylesheet");  
     Page.Header.Controls.Add(newStyleSheet);  
 }  

3- 将此行添加到您的 page_load 事件中:

UpdateStylesheet("dark.css");  

4- 像这样处理 SwitchStylesheets:

 if (radioDark.Checked)  
        UpdateStylesheet("dark.css");   
    if (radioLight.Checked)  
        UpdateStylesheet("light.css");   

关于c# - 当页面在单击按钮时加载动态内容时,页脚 CSS 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4059485/

相关文章:

c# - Entity Framework : Run EF Migrations for Previous Version in Net Core

c# - C# 和 foreach 中的枚举

python - 将特殊方法动态分配给对象而不是 Python 中的类

html - 垂直对齐仅一个行内 block 元素

javascript - 两个 Css 文件具有相同的类 - 从特定类中删除类,如何?

c# - 当我在 Using(connection) 的代码中返回时,连接和数据库对象是否被正确关闭

c# - 锁助手的线程安全使用(关于内存屏障)

javascript - Angular - 如何通过路由器将信息传递到 Controller ?

html - 具有动态粘性页脚的站点布局,可返回到 IE6,没有 JS : Is it possible?

css - 水平堆叠 anchor 标签到 100% 的容器