2010-03-14 50 views
0

我正在爲特定目的構建自定義Web面板控件。我希望控件可以固定爲特定的寬度和高度,以便不會在設計模式和屬性窗口中調整大小。我怎樣才能做到這一點。如何創建不可調整大小的自定義服務器控件

這是非常重要和緊迫的,我會很感激,如果你能幫助我。

回答

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

} 
+0

Perfect.I正在尋找this.For屬性窗口也工作正常,但你必須在構造函數中設置寬度和高度,就像@slugster所做的那樣。 – wonde 2010-03-25 23:16:26

1

如果你讓你的用戶控件繼承自System.Web.UI.WebControls.WebControl,那麼你可以覆蓋高度和寬度屬性,並且在設置器中什麼都不做。

所以我創建了一個新的控制,把它稱爲zzz,並從改變其遺傳System.Web.UI.UserControlSystem.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 { } 
    } 
} 
+0

@Slugster - 那麼我試過這種方式,但仍然是控制是可調整大小的。我現在需要的是在設計時以及從屬性窗口中調整大小的不可調整大小。得到它。 – wonde 2010-03-14 21:21:59

相關問題