English 中文(简体)
从代码后台更改ASCX的边框属性?
原标题:Change border properties of ASCX from code-behind?

我正在构建一个ASP.NET用户控件库,我正在从自定义的UserControlBase类中派生它们,而这个类进一步派生自实际的UserControl类。层次结构看起来像这样:

ASCX -> UserControlBase:UserControl

我有一个需求要在所有ASCX周围放置一个边框。因此,我想如果我可以修改UserControlBase,它将适用于所有ASCX。我尝试在UserControlBase的Page_Load中尝试以下代码,但它不起作用。

这一点。 Attributes.Add(“模版”),“边界-color:#FF66; 边界-width:4px; 边界式:Dashed;”

我该怎么做才能让它生效?请给予建议。

Thanks AJ

最佳回答

用户控件除了您放置在内部的内容之外没有任何与它们相关联的标记。因此,没有标记可添加您的样式属性。因此,您必须自己添加一个包装标记。

解决方案之一是通过覆盖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>");
}

这把你对用户的控制总结在一个包括你试图补充的风格的圆顶上。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签