C# 当页面在单击按钮时加载动态内容时,页脚CSS不起作用 .html (空)
C# 当页面在单击按钮时加载动态内容时,页脚CSS不起作用 .html (空),c#,dynamic,css,C#,Dynamic,Css,.cs 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControl; 公共部分类\u默认值:System.Web.UI.Page { 受保护的无效页面加载(对象发送方、事件参数e) { } 受保护的void SwitchStylesheets(对象发送方、事件参数e) { 如果(radioDark.选中) stylesheet.Href=“
.cs
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的void SwitchStylesheets(对象发送方、事件参数e)
{
如果(radioDark.选中)
stylesheet.Href=“dark.css”;
如果(radioLight.Checked)
stylesheet.Href=“light.css”;
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
int count=DateTime.Now.Second;
for(int i=0;i ”;
标签空间=新标签();
space.Text=spacee;
表1.对照添加(q);
表格1.控件.添加(空格);
}//为了
}
}
单击按钮时,它会正常工作,但页脚不会注册页面的扩展。您的代码完全错误。即使autopostback设置为true,也不能像这样更改控件的样式表。
更新:以下是您应该如何操作: 1-从页面中删除.css引用。
2-将此方法添加到您的页面:
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
}
}
3-将此行添加到页面加载事件:
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);
}
4-按如下方式处理SwitchStylesheet:
UpdateStylesheet("dark.css");
UpdateStylesheet("dark.css");
if (radioDark.Checked)
UpdateStylesheet("dark.css");
if (radioLight.Checked)
UpdateStylesheet("light.css");