从代码隐藏更改 ASCX 的边框属性?

发布于 2024-08-23 04:13:35 字数 401 浏览 4 评论 0原文

我正在构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小姐丶请自重 2024-08-30 04:13:35

除了您放入其中的内容之外,用户控件没有任何与之关联的标记。因此,没有可以添加样式属性的标签。所以你必须自己添加一个包装标签。

一种解决方案是重写 UserControlBase 的 Render 方法,如下所示:

protected override void Render(HtmlTextWriter writer)
{
    writer.Write("<div style='border-color:#FFFF66;border-width:4px;border-style:Dashed'>");
    base.Render(writer);
    writer.Write("</div>");
}

这会将您的用户控件包装在包含您尝试添加的样式属性的 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:

protected override void Render(HtmlTextWriter writer)
{
    writer.Write("<div style='border-color:#FFFF66;border-width:4px;border-style:Dashed'>");
    base.Render(writer);
    writer.Write("</div>");
}

This wraps your user control in a div tag that includes the style attributes you are trying to add.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文