开发者

Change border properties of ASCX from code-behind?

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜