Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom ASP.NET Container Control

I've been trying to create a custom control that works exactly like the Panel control except surrounded by a few divs and such to create a rounded box look. I haven't been able to find a decent example of how to do this.

I need to be able to place text and controls inside the control and access it directly without referencing the panel (exactly the way the Panel control works).

Does anyone have any examples of this?

like image 748
Arthur Chaparyan Avatar asked Nov 20 '08 18:11

Arthur Chaparyan


People also ask

Why would you use custom controls?

Controls are useful when you're developing complex applications because you can avoid code duplication. Because custom controls are objects, you can use the typical features offered by OOP. You can start from scratch with your own control, or use an existing control and enrich it.

What is container control in asp net?

ASP.NET MVC 5 for BeginnersThe Panel control works as a container for other controls on the page. It controls the appearance and visibility of the controls it contains. It also allows generating controls programmatically.

What is custom in ASP net?

Custom control is a control that is not included in the . NET framework library and is instead created by a third-party software vendor or a user. Custom control is a concept used while building both Windows Forms client and ASP.NET Web applications.


1 Answers

There are two ways to do this. One is to implement INamingContainer on your control, and it takes a lot of effort.

The other way is to inherit from Panel, and override the RenderBeginTag and RenderEndTag methods to add your custom markup. This is easy.

public class RoundedCornersPanel : System.Web.UI.WebControls.Panel
{
    public override RenderBeginTag (HtmlTextWriter writer)
    {
        writer.Write("Your rounded corner opening markup");
        base.RenderBeginTag(writer);
    }

    public override RenderEndTag (HtmlTextWriter writer)
    {
        base.RenderEndTag(writer);
        writer.Write("Your rounded corner closing markup");                     
    }
}
like image 132
FlySwat Avatar answered Sep 26 '22 02:09

FlySwat