在使用的theme的过程中,有的时候避免不了要动态的修改控件的skinID, 在设置SKInID的时候,跟设置theme一样,都需要在页面的PreInit 事件中来实现。
因为在页面的生命周期里PreInit 事件是在控件的初始化之前触发的, 因此在对于一个普通的页面, 可以这样动态的设置skinID:

 protected void Page_PreInit(Object sender, EventArgs e)
    {
        Page.Theme = "Blue";
        Label c = new Label();
        c.SkinID = "lbl";
        c.Text = "11111111111111111111111111111";
        this.Panel1.Controls.Add(c);

    }


但是,如果这个页面使用了master page, 就会出现这样的错误:Object reference not set to an instance of an object.
这是因为页面中的控件是不可用的,在master page的控件树实例化之前,因此在我们要访问PreInit事件,并必须先实例化master page:

protected void Page_PreInit(Object sender, EventArgs e)
    {
        System.Web.UI.MasterPage m = Master;

        Label c = new Label();
        c.SkinID = "lbl";
        c.Text = "11111111111111111111111111111";
        this.Panel1.Controls.Add(c);

    }

这样,就可以正常运行了。 

相关文章:

  • 2022-12-23
  • 2021-08-16
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-06
  • 2022-12-23
  • 2021-08-09
  • 2021-11-06
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案