WebPartDesigner 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供設計時的視覺化控制支援 WebPart 。
public ref class WebPartDesigner : System::Web::UI::Design::WebControls::WebParts::PartDesigner
public class WebPartDesigner : System.Web.UI.Design.WebControls.WebParts.PartDesigner
type WebPartDesigner = class
inherit PartDesigner
Public Class WebPartDesigner
Inherits PartDesigner
- 繼承
範例
以下程式碼範例展示了控制項與其對應WebPart的 之間的互動WebPartDesigner。 控制 WebPart 項包含 Calendar 一個控制項,供最終使用者選擇生日,一個 Button 用來提交選擇的控制項,以及 Label 一個顯示使用者生日訊息的控制項。 該控制器 WebPartDesigner 會驗證所屬控制的類型是否符合預期類型,並自訂該控制點的設計時渲染方式。 請注意,設計師的視覺自訂僅在設計時可見,而相關控制項的自訂則在執行時與設計時皆可見。
本範例中覆寫的所有方法皆源自基 ControlDesigner 底類別。 關於其他可用成員及其使用情況,請參見 System.Web.UI.Design.ControlDesigner。
using System;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.Design.WebControls.WebParts;
using System.ComponentModel;
/// <summary>
/// BirthdayPart demonstrates some of the most
/// common overrides of members of the WebPart
/// class. BirthdayPartDesigner shows how to
/// customize the rendering of a custom WebPart
/// control.
/// </summary>
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand,
Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level=AspNetHostingPermissionLevel.Minimal)]
[Designer(typeof(BirthdayPartDesigner))]
public class BirthdayPart : WebPart
{
private DateTime birthDate;
Calendar input;
Label displayContent;
public BirthdayPart()
{
this.AllowClose = false;
this.Title = "Enter your birthday";
}
[
Personalizable(PersonalizationScope.User, true),
WebBrowsable()
]
public DateTime BirthDate
{
get { return birthDate; }
set { birthDate = value; }
}
// Set the appearance of the control at run time.
protected override void CreateChildControls()
{
Controls.Clear();
input = new Calendar();
this.Controls.Add(input);
Button update = new Button();
update.Text = "Submit";
update.Click += new EventHandler(this.submit_Click);
this.Controls.Add(update);
displayContent = new Label();
displayContent.BackColor =
System.Drawing.Color.LightBlue;
Literal br = new Literal();
br.Text = "<br />";
if ((this.birthDate.Day == DateTime.Now.Day)
&& (this.birthDate.Month == DateTime.Now.Month))
{
displayContent.Text = "Happy Birthday!";
this.Controls.Add(br);
this.Controls.Add(displayContent);
}
}
private void submit_Click(object sender, EventArgs e)
{
this.birthDate = input.SelectedDate;
}
}
public class BirthdayPartDesigner : WebPartDesigner
{
public override void Initialize(IComponent component)
{
// Verify that the associated control is a BirthdayPart.
if (!typeof(BirthdayPart).IsInstanceOfType(component))
{
throw new ArgumentException("The specified control is not of type 'BirthdayPart'", "component");
}
base.Initialize(component);
}
// Here is where you make customizations
// to design time appearance that will not
// be visible to the end user.
public override string GetDesignTimeHtml()
{
string designTimeHtml = null;
try
{
designTimeHtml = base.GetDesignTimeHtml();
string s = "<hr /><hr />I just added these lines to the"
+ " bottom of the control.<hr /><hr /></div>";
designTimeHtml = designTimeHtml.Replace("</div>", s);
}
catch (Exception ex)
{
designTimeHtml = GetErrorDesignTimeHtml(ex);
}
finally
{
// undo any changes in the try block
}
if ((designTimeHtml == null) || (designTimeHtml.Length == 0))
{
designTimeHtml = GetEmptyDesignTimeHtml();
}
return designTimeHtml;
}
// This method normally returns a blank string.
// Override to return a meaningful message.
protected override string GetEmptyDesignTimeHtml()
{
return CreatePlaceHolderDesignTimeHtml(
"<hr />If the page developer forgot to fill in a " +
"required property you could tell them here.<hr />");
}
// Add specific text to the generic error message.
protected override string GetErrorDesignTimeHtml(Exception e)
{
string s = "<hr />The control failed to render.";
return(s + e.Message + "<hr />");
}
}
}
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.Design.WebControls.WebParts
Imports System.ComponentModel
' BirthdayPart demonstrates some of the most
' common overrides of members of the WebPart
' class. BirthdayPartDesigner shows how to
' customize the rendering of a custom WebPart
' control.
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, Level:=AspNetHostingPermissionLevel.Minimal)> _
<Designer(GetType(BirthdayPartDesigner))> _
Public Class BirthdayPart
Inherits WebPart
Private m_birthDate As DateTime
Private input As Calendar
Private displayContent As Label
Public Sub New()
Me.AllowClose = False
Me.Title = "Enter your birthday"
End Sub
<Personalizable(PersonalizationScope.User, True), WebBrowsable()> _
Public Property BirthDate() As DateTime
Get
Return m_birthDate
End Get
Set(value As DateTime)
m_birthDate = value
End Set
End Property
' Set the appearance of the control at run time.
Protected Overrides Sub CreateChildControls()
Controls.Clear()
input = New Calendar()
Me.Controls.Add(input)
Dim update As New Button()
update.Text = "Submit"
AddHandler update.Click, New EventHandler(AddressOf Me.submit_Click)
Me.Controls.Add(update)
displayContent = New Label()
displayContent.BackColor = System.Drawing.Color.LightBlue
Dim br As New Literal()
br.Text = "<br />"
If (Me.m_birthDate.Day = DateTime.Now.Day) AndAlso (Me.m_birthDate.Month = DateTime.Now.Month) Then
displayContent.Text = "Happy Birthday!"
Me.Controls.Add(br)
Me.Controls.Add(displayContent)
End If
End Sub
Private Sub submit_Click(sender As Object, e As EventArgs)
Me.m_birthDate = input.SelectedDate
End Sub
End Class
Public Class BirthdayPartDesigner
Inherits WebPartDesigner
Public Overrides Sub Initialize(component As IComponent)
' Verify that the associated control is a BirthdayPart.
If Not GetType(BirthdayPart).IsInstanceOfType(component) Then
Throw New ArgumentException("The specified control is not of type 'BirthdayPart'", "component")
End If
MyBase.Initialize(component)
End Sub
' Here is where you make customizations
' to design time appearance that will not
' be visible to the end user.
Public Overrides Function GetDesignTimeHtml() As String
Dim designTimeHtml As String = Nothing
Try
designTimeHtml = MyBase.GetDesignTimeHtml()
Dim s As String = "<hr /><hr />I just added these lines to the" & " bottom of the control.<hr /><hr /></div>"
designTimeHtml = designTimeHtml.Replace("</div>", s)
Catch ex As Exception
designTimeHtml = GetErrorDesignTimeHtml(ex)
' undo any changes in the try block
Finally
End Try
If (designTimeHtml Is Nothing) OrElse (designTimeHtml.Length = 0) Then
designTimeHtml = GetEmptyDesignTimeHtml()
End If
Return designTimeHtml
End Function
' This method normally returns a blank string.
' Override to return a meaningful message.
Protected Overrides Function GetEmptyDesignTimeHtml() As String
Return CreatePlaceHolderDesignTimeHtml("<hr />If the page developer forgot to fill in a " & "required property you could tell them here.<hr />")
End Function
' Add specific text to the generic error message.
Protected Overrides Function GetErrorDesignTimeHtml(e As Exception) As String
Dim s As String = "<hr />The control failed to render."
Return (s & e.Message & "<hr />")
End Function
End Class
End Namespace
備註
該 WebPartDesigner 類別在設計時提供控制項的視覺化表示 WebPart 。 它是從 PartDesigner 類別衍生出來的,並加入驗證以驗證所屬控制是否為 WebPart 控制或衍生型別。
WebPartDesigner 從 UsePreviewControl 父節點繼承一個屬性,該屬性總是設為 true。 這會使視覺設計環境產生 View 一個控制項,暫時保存在設計表面上的 副本 WebPart ;這個副本接著會被持久化成標記。 如果你覆寫屬性 UsePreviewControl 返回 false,視覺設計環境會直接從實際 WebPart 控制項產生標記。
網頁零件設計師通常與一般控制設計師類似;設定設計時外觀的主要方法源自該 ControlDesigner 類別。 具體來說,你可以覆寫 GetDesignTimeHtml 改變控制點設計時標記的方法。 你也可以覆寫 GetErrorDesignTimeHtml 和 GetEmptyDesignTimeHtml 方法,分別處理錯誤和空字串(「」)。
建構函式
| 名稱 | Description |
|---|---|
| WebPartDesigner() |
初始化 WebPartDesigner 類別的新執行個體。 |
屬性
| 名稱 | Description |
|---|---|
| ActionLists |
取得控制設計器的動作清單集合。 (繼承來源 ControlDesigner) |
| AllowResize |
會獲得一個值,表示控制項是否能在設計時環境中調整大小。 (繼承來源 ControlDesigner) |
| AssociatedComponents |
取得設計師管理的與該元件相關的元件集合。 (繼承來源 ComponentDesigner) |
| AutoFormats |
取得預設自動格式化方案集合,於設計時顯示於相關控制項的 自動格式 化對話框中。 (繼承來源 ControlDesigner) |
| Behavior |
已淘汰.
取得或設定與設計器相關的 DHTML 行為。 (繼承來源 HtmlControlDesigner) |
| Component |
取得設計師正在設計的元件。 (繼承來源 ComponentDesigner) |
| DataBindings |
取得目前控制項的資料綁定收集。 (繼承來源 HtmlControlDesigner) |
| DataBindingsEnabled |
取得一個值,表示相關控制項的包含區域是否支援資料綁定。 (繼承來源 ControlDesigner) |
| DesignerState |
取得一個物件,用於在設計時持久化相關控制項的資料。 (繼承來源 ControlDesigner) |
| DesignTimeElement |
已淘汰.
取得代表該物件在設計表面上所關聯 HtmlControlDesigner 控制項的設計時物件。 (繼承來源 HtmlControlDesigner) |
| DesignTimeElementView |
已淘汰.
取得控制設計器的視控物件。 (繼承來源 ControlDesigner) |
| DesignTimeHtmlRequiresLoadComplete |
已淘汰.
會取得一個值,表示設計主機是否必須完成載入才能呼叫該 GetDesignTimeHtml 方法。 (繼承來源 ControlDesigner) |
| Expressions |
在設計時取得目前控制項的表達式綁定。 (繼承來源 HtmlControlDesigner) |
| HidePropertiesInTemplateMode |
會取得一個值,表示當控制項處於模板模式時,相關控制項的屬性是否被隱藏。 (繼承來源 ControlDesigner) |
| ID |
取得或設定控制項的 ID 字串。 (繼承來源 ControlDesigner) |
| InheritanceAttribute |
會獲得一個屬性,表示相關元件繼承的類型。 (繼承來源 ComponentDesigner) |
| Inherited |
會得到一個值,表示該元件是否為繼承。 (繼承來源 ComponentDesigner) |
| InTemplateMode |
會獲得一個值,表示控制項在設計主機中是處於範本檢視模式還是編輯模式。 該 InTemplateMode 屬性為唯讀。 (繼承來源 ControlDesigner) |
| IsDirty |
已淘汰.
取得或設定一個值,表示網頁伺服器控制項是否被標記為變更。 (繼承來源 ControlDesigner) |
| ParentComponent |
取得這個設計器的父元件。 (繼承來源 ComponentDesigner) |
| ReadOnly |
已淘汰.
取得或設定一個值,表示控制項屬性在設計時是否為唯讀。 (繼承來源 ControlDesigner) |
| RootDesigner |
取得包含相關控制項的網頁表單頁面的控制項設計器。 (繼承來源 ControlDesigner) |
| ShadowProperties |
會取得一組屬性值,覆蓋使用者設定。 (繼承來源 ComponentDesigner) |
| ShouldCodeSerialize |
已淘汰.
在序列化過程中,會取得或設定一個值,指示是否要在目前設計文件的程式碼背後檔案中為控制項建立欄位宣告。 (繼承來源 HtmlControlDesigner) |
| Tag |
取得一個代表相關控制項 HTML 標記元素的物件。 (繼承來源 ControlDesigner) |
| TemplateGroups |
獲得一組模板群組,每個群組包含一個或多個範本定義。 (繼承來源 ControlDesigner) |
| UsePreviewControl |
會獲得一個值,指示設計師是否應該使用暫存複製品,而非與設計師實際相關的控制項來產生設計時標記。 (繼承來源 PartDesigner) |
| Verbs |
取得與設計者相關元件所支援的設計時動詞。 (繼承來源 ComponentDesigner) |
| ViewControl |
取得或設定一個網頁伺服器控制項,可用於預覽設計時的 HTML 標記。 (繼承來源 ControlDesigner) |
| ViewControlCreated |
取得或設定一個值,指示是否 |
| Visible |
會獲得一個值,表示該控制點在設計時是否可見。 (繼承來源 ControlDesigner) |
方法
明確介面實作
| 名稱 | Description |
|---|---|
| IDesignerFilter.PostFilterAttributes(IDictionary) |
關於此成員的描述,請參見方法。PostFilterAttributes(IDictionary) (繼承來源 ComponentDesigner) |
| IDesignerFilter.PostFilterEvents(IDictionary) |
關於此成員的描述,請參見方法。PostFilterEvents(IDictionary) (繼承來源 ComponentDesigner) |
| IDesignerFilter.PostFilterProperties(IDictionary) |
關於此成員的描述,請參見方法。PostFilterProperties(IDictionary) (繼承來源 ComponentDesigner) |
| IDesignerFilter.PreFilterAttributes(IDictionary) |
關於此成員的描述,請參見方法。PreFilterAttributes(IDictionary) (繼承來源 ComponentDesigner) |
| IDesignerFilter.PreFilterEvents(IDictionary) |
關於此成員的描述,請參見方法。PreFilterEvents(IDictionary) (繼承來源 ComponentDesigner) |
| IDesignerFilter.PreFilterProperties(IDictionary) |
關於此成員的描述,請參見方法。PreFilterProperties(IDictionary) (繼承來源 ComponentDesigner) |
| ITreeDesigner.Children |
關於此成員的描述,請參見 Children 該物業。 (繼承來源 ComponentDesigner) |
| ITreeDesigner.Parent |
關於此成員的描述,請參見 Parent 該物業。 (繼承來源 ComponentDesigner) |