views:

75

answers:

2

Hi guys,

I am building a custom web panel control for specific purpose.I want the control be fixed with specific width and height so that not to be resized in design mode as well as from property window.How can I do this.

It is very important and urgent.I will appreciate if you can help me.

+1  A: 

If you make your usercontrol inherit from System.Web.UI.WebControls.WebControl, then you can override the height and width properties, and do nothing in the setter.

So i created a new control, called it zzz, and changed its inheritance from System.Web.UI.UserControl to System.Web.UI.WebControls.WebControl. After that, this is what my code behind looks like:

public partial class zzz : WebControl
{
    public zzz()
    {
        base.Height = new Unit(100, UnitType.Pixel);
        base.Width = new Unit(150, UnitType.Pixel);
    }

    public override Unit Height
    {
        get { return base.Height; }
        set { }
    }

    public override Unit Width
    {
        get { return base.Width; }
        set {  }
    }
}
slugster
@Slugster -Well I tried this way.But still the control is resizable.What I need now is a fixed size non resizable at design time as well as from property window. Get it.
Wonde
+1  A: 

Try this:

Control Designer

public class CustomPanelDesigner : ControlDesigner
{

    public override bool AllowResize
    {
        get { return false; }
    }

}

Custom Control

[Designer(typeof(CustomPanelDesigner))]
public class CustomPanel : WebControl
{

    public CustomPanel() : base(HtmlTextWriterTag.Div) { }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public override Unit Width
    {
        get { return new Unit("100px"); }
        set { throw new NotSupportedException(); }
    }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public override Unit Height
    {
        get { return new Unit("100px"); }
        set { throw new NotSupportedException(); }
    }

}
Mehdi Golchin
Perfect.I was looking this.For the property window is also works fine,but you have to set the width and height in the constructor like @slugster did it.
Wonde