代码之家  ›  专栏  ›  技术社区  ›  user287745

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

  •  0
  • user287745  · 技术社区  · 15 年前

    <%@ 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
        }
    }
    

    1 回复  |  直到 8 年前
        1
  •  1
  •   Kamyar    15 年前

    你的代码完全错了。即使autopostback设置为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);  
     }  
    

    UpdateStylesheet("dark.css");  
    

    4-像这样处理SwitchStylesheets:

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