从代码隐藏更改 ASCX 的边框属性?
我正在构建一个 asp.net 用户控件库,该库是从自定义 UserControlBase 类派生的,该类进一步派生自实际的 UserControl 类。层次结构如下所示:
ASCX -> UserControlBase :UserControl
我需要在所有 ASCX 周围放置边框。所以,我想如果我可以修改 UserControlBase 它将适用于所有 ASCX。我尝试在 UserCOntrolBase 的 Page_Load 中执行以下代码,但它不起作用
this.Attributes.Add("style", "border-color:#FFFF66;border-width:4px;border-style:Dashed;");
我应该做什么才能让它发挥作用?请指教。
谢谢 阿杰
I am building a library of asp.net user controls which I am deriving from a custom UserControlBase class which further derives from actual UserControl class. Hierarchy looks like this :
ASCX -> UserControlBase : UserControl
I have this requirement to put a border around all the ASCX's. So, I thought if I can modify UserControlBase it will apply to all ASCXs. I tried following code in Page_Load of UserCOntrolBase but its not working
this.Attributes.Add("style", "border-color:#FFFF66;border-width:4px;border-style:Dashed;");
What should I do to make it work? Please advise.
Thanks
AJ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了您放入其中的内容之外,用户控件没有任何与之关联的标记。因此,没有可以添加样式属性的标签。所以你必须自己添加一个包装标签。
一种解决方案是重写 UserControlBase 的 Render 方法,如下所示:
这会将您的用户控件包装在包含您尝试添加的样式属性的 div 标记中。
User controls don't have any markup associated with them other than what you put inside. So there's no tag to which you can add your style attributes. So you have to add a wrapping tag yourself.
One solution is to override the Render method of UserControlBase like this:
This wraps your user control in a div tag that includes the style attributes you are trying to add.