gpt4 book ai didi

c# - 用户控件中的主题

转载 作者:行者123 更新时间:2023-11-30 22:39:22 27 4
gpt4 key购买 nike

如何在 UserControl 中动态更改 Page.Theme

据我所知,它可以在 Page_PreInit 中完成,但是 UserControl 没有这样的事件,它只存在于 Page 类中。

最佳答案

MSDN says :

You must be aware of one restriction when using the theme property. The theme property can only be set during or before the Page PreInit event.

用户控件生命周期在页面的 PreInit 事件之后立即开始,因此您无法直接从控件设置主题。

但仍然有一些解决方法:假设当前主题存储在 session 对象中,您可以在用户控件的任何位置更改此 session 值,然后只需刷新页面即可,例如通过使用 Response.Redirect(Request.Url.AbsoluteUri) 并更改 Page_PreInit 处理程序中的主题:

这是页面的 PreInit 事件处理程序:

protected void Page_PreInit(object sender, EventArgs e)
{
var theme = Session["Theme"] as string;
if (theme != null)
{
Page.Theme = theme;
}
}

例如OnSelectedIndexChanged 用户控件中的事件处理程序:

protected void ddlTheme_SelectedIndexChanged(object sender, EventArgs e)
{
Session["Theme"] = ddlTheme.SelectedValue;
Response.Redirect(Request.Url.AbsoluteUri);
}

关于c# - 用户控件中的主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5788819/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com