如何创建不可调整大小的自定义服务器控件

时间:2010-03-14 05:29:14

标签: asp.net user-controls custom-controls

我正在为特定目的构建自定义Web面板控件。我希望控件可以使用特定的宽度和高度进行修复,以便在设计模式和属性窗口中不会调整大小。我该怎么办呢。

这是非常重要和紧迫的。如果你能帮助我,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果使usercontrol继承自System.Web.UI.WebControls.WebControl,则可以覆盖height和width属性,并在setter中不执行任何操作。

所以我创建了一个名为zzz的新控件,并将其继承从 System.Web.UI.UserControl 更改为 System.Web.UI.WebControls.WebControl 。在那之后,这就是我背后的代码:

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 {  }
    }
}

答案 1 :(得分:1)

试试这个:

控制设计器

public class CustomPanelDesigner : ControlDesigner
{

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

}

自定义控制

[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(); }
    }

}