简体   繁体   English

如何在用户控件中访问子控件的HtmlControl.Style?

[英]How do I access sub-control's HtmlControl.Style in user control?

I have a user control that contains the following text (among other things) in the .ascx file: 我有一个用户控件,它在.ascx文件中包含以下文本(除其他外):

<div id="divLine" runat="server">
    <-- Stuff -->
</div>

In the C# code, I have this property: 在C#代码中,我具有以下属性:

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public CssStyleCollection LeftStyle
{
    get { return divLeft.Style; }
}

This is pretty much copied from System.Web.UI.HtmlControls.HtmlControl.cs . 这几乎是从System.Web.UI.HtmlControls.HtmlControl.cs复制的。 However, when trying to use my custom control in .aspx pages, I can't seem to access <uwc:UserControl LeftStyle="stuff"> like I could access it directly in <div style="stuff"> . 但是,当尝试在.aspx页面中使用自定义控件时,似乎无法访问<uwc:UserControl LeftStyle="stuff">就像我可以直接在<div style="stuff">访问它一样。 Is there any way to? 有什么办法吗?

No answer, so I took a different approach. 没有答案,所以我采取了另一种方法。 divLine only contained an asp.net Label , which I exposed as a public property of the control. divLine仅包含一个asp.net Label ,我将其公开为控件的公共属性。 Apparently you can do things like <asp:Label runat="server" style="stuff"/> . 显然,您可以执行<asp:Label runat="server" style="stuff"/> Intellisense will not indicate that style is a valid attribute, but it can still be used. Intellisense不会指示style是有效的属性,但仍可以使用。 Just like other asp.net controls, attributes the IDE doesn't recognize will still be written to HTML. 就像其他asp.net控件一样,IDE无法识别的属性仍将写入HTML。 Heck, once you get style=" written, you'll even get the typical drop-down guide for it. 哎呀,一旦您编写了style=" ,就可以得到典型的下拉指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM