无法动态更改 SkinID 属性
当我尝试让我的 C# 类更改 asp 控件的外观时,出现此错误:
“SkinId”属性只能在 Page_PreInit 中或之前设置 静态控件的事件。对于动态控件,请先设置属性 将其添加到 Controls 集合中。
我的目标是在每个页面上提供一个面板,将其称为 ID="response",然后动态地将其 CSS 类从错误更改为成功,或从成功更改为错误(因此它是红色或绿色)。当创建响应时,我还使其可见= true。
显然,我被迫使用 CssClass 属性,这是唯一可行的方法。
作为一个题外话: 在 PHP 中,您不会遇到使用不同的“预初始化”、“后初始化”等的问题。这是一个完全不必要的过程。您只需更改 html,然后将其发送回用户即可。我有点困惑为什么 ASP.NET 决定让一切变得过于复杂。对我来说,花时间学习所有这些不同的复杂过程来简单地显示一个网页有点愚蠢。学习微软上难以阅读的 ASP 生命周期文档中写的所有怪癖需要时间。并不是要侮辱任何微软人,但这不切实际。
I get this error when I try to have my C# class change the skin of an asp control:
The 'SkinId' property can only be set in or before the Page_PreInit
event for static controls. For dynamic controls, set the property before
adding it to the Controls collection.
My goal is to provide a panel, call it ID="response", on every page, and then dynamically change it's CSS class from Error to Success, or Success to Error (so it's red or green). And also I make it visible = true, when a response is created.
Apparently, I am forced to use CssClass attribute, which is the only way this will work.
As a side-off-topic note:
In PHP, you would not have a problem of using different "pre-init" "post-init" etc. A completely unnecessary process. You would simply change the html before you send it back to the user. I'm a bit confused why ASP.NET decides to overcomplicate everything. It's a bit silly for me to take time to learn all these different complicated processes to simply display a webpage. It takes time to learn all the quirks written in difficult-to-read ASP life-cycle documents on microsoft. Not to insult any microsoft people, but it's just not practical.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果它是静态控件,即您在 .aspx 页面中定义面板,则更改 SkinId 的唯一位置是在 PreInit 方法中,例如:
当然,仅当页面第一次出现时才会调用 PreInit 方法已初始化——不在回发上。
您可以将要使用的 SkinId 保存到 ViewState,然后调用 Response.Redirect("myPage.aspx")...,如上所示,从 ViewState 中获取 SkinId 字符串并相应地设置面板 SkinId。
或者,不要使用面板,而是尝试使用 .Net Ajax 库中的 UpdatePanel。单击 UpdatePanel 中的按钮(假设其设置为触发 ASyncPostBack)将运行 OnPreInit 方法。
也就是说,如果您要更改背景,使用 CssClass 属性将是最有效的方法。
If it is a static control, that is you are defining the Panel in your .aspx page, then the only place to change the SkinId is in the PreInit method e.g.:
Of Course, the PreInit method is only called when the Page is being first Initialized -- not on a PostBack.
You could save the skinId you wanted to use to the ViewState and then call a Response.Redirect("myPage.aspx")... and as seen above grab the skinId string from the ViewState and set the Panel skinId accordingly.
Alternatively, rather than using a Panel try using an UpdatePanel from the .Net Ajax library. Clicking a button in the UpdatePanel (provided it's setup to Trigger an ASyncPostBack) will run the OnPreInit method.
That said, provided you are changing the background, going with the CssClass property would be the most efficient way to do this.
ASP 及其子 ASP.NET 基本上是对普通 HTML 和 IIS 页面呈现器的巨大改造。它与 IIS 中已经存在的生命周期的各个阶段挂钩,而不是像 PHP 那样拥有自己的生命周期。因此,您可以在某些领域做一些事情,因为它所依赖的事情要么不是一成不变的,因此您可以更改它们,要么是您可以使用它们。 ASP.NET 的强大功能在于与 .NET 类和 .NET Framework 的互操作,IMO 弥补了它的一些特性。
无论如何,皮肤是主题的一部分,它们在过程的早期加载,因此可以使用适当的默认样式来初始化控件。这就是关键;主题在 PreInit 之后被锁定,但是外观后面的样式(和 CssClasses)可以编辑,包括 PreRender,其中包括事件处理程序(触发验证)。因此,动态设置 Style 或 CssClass。
要在没有完整回发的情况下完成此操作,您可以将应更改颜色的控件放入 AJAX UpdatePanel 中,该控件可以与页面的其他元素分开重新呈现,并且将保留其当前内容,直到通过 JavaScript 修改 DOM客户端。
ASP, and its child ASP.NET, is basically a huge hack of vanilla HTML and the IIS page renderer. It hooks into various stages of the lifecycle that already existed in IIS, rather than having its own lifecycle like PHP. As such, there are things you can do in certain areas because the things it depends on either aren't set in stone so you can change them, or are so you can work with them. The great power of ASP.NET, which is the interop with .NET classes and the .NET Framework, IMO makes up for some of its idiosyncracies.
Anyway, Skins are part of Themes, which are loaded early in the process so the controls can be initialized with their proper default Styles. That's the key; the Theme is locked after PreInit, but the Styles (and CssClasses) behind the Skins are editable right up to and including PreRender, which includes event handlers (which fire validation). So, set the Style or the CssClass dynamically.
To do it without a full postback, you can put the controls that should change color in an AJAX UpdatePanel, which can be re-rendered separately from the other elements of the page and will keep its current contents until the DOM is modified via the JavaScript client-side.
设置 CssClass 属性与使用 PHP 所做的事情非常接近,那么为什么不直接这样做呢?
Skin 文件的两个真正好处是为所有控件设置默认值(根本没有 SkinId)或设置无法使用 css 控制的属性。
Setting the CssClass attribute is much closer to what you'd do with PHP, so why not just do that?
The two real benefits of Skin files are setting defaults for all controls (no skinId at all) or setting properties that can't be controlled with css.