単純な Web フォーム コントロール デザイナの実装
Web フォームのデザイナ クラスは、デザイナ ホストにデザイン時 HTML を提供することにより、コントロールのデザイン時の表示を制御します。.NET Framework には、基本クラス System.Web.UI.ControlDesigner が用意されています。この基本クラスは、サーバー コントロール デザイナの基本機能を提供します。ControlDesigner は、デザイン時 HTML を表示するための次のような重要なメソッドを定義します。
Public Class ControlDesigner
Inherits HtmlControlDesigner
Public Overridable Function GetDesignTimeHtml() As String
...
End Function
Protected Overridable Function GetEmptyDesignTimeHtml() As String
...
End Function
Protected Overridable Function GetErrorDesignTimeHtml(e As Exception) As String
...
End Function
' The helper method that provides default placeholder HTML
' cannot be overridden. Invoke this method when it is not possible to
' render a meaningful representation of the control at design time.
Protected Function CreatePlaceHolderDesignTimeHtml(s As String) As String
...
End Function
End Class
[C#]
public class ControlDesigner : HtmlControlDesigner {
public virtual string GetDesignTimeHtml() {...}
protected virtual string GetEmptyDesignTimeHtml() {...}
protected virtual string GetErrorDesignTimeHtml(Exception e) {...}
// The helper method that provides default placeholder HTML
// cannot be overridden. Invoke this method when it is not possible to
// render a meaningful representation of the control at design time.
protected string CreatePlaceHolderDesignTimeHtml(string s) {...}
}
GetDesignTimeHtml メソッドは、コントロールのデザイン時 HTML を返します。GetDesignTimeHtml の基本実装によってコントロールの Render メソッドを呼び出し、実行時と同じ HTML をデザイン時にも表示します。このデザイナのタスクは、GetDesignTimeHtml をオーバーライドして、より適切な HTML をデザイン時に表示することです。
GetEmptyDesignTimeHtml メソッドは、プロパティが設定されていないなどの理由でコントロールが表示する実行時 HTML 文字列が空の場合に、デザイン時に表示する HTML 文字列を提供します。GetEmptyDesignTimeHtml メソッドの基本実装は、コントロールの完全限定名と ID を返します。デザイナは、このメソッドをオーバーライドして CreatePlaceHolderDesignTimeHtml を呼び出すか、または代替 HTML を返すことができます。
GetErrorDesignTimeHtml メソッドは、エラーが発生したときにコントロールのデザイン時の表示に使用する HTML を提供します。
単純な Web フォーム コントロール デザイナを実装するには、次のようにします。
- System.Web.UI.ControlDesigner から直接または間接的に派生するクラスを定義します。
- (省略可能) ControlDesigner から継承された GetDesignTimeHtml メソッドをオーバーライドします。
- (省略可能) ControlDesigner から継承された GetEmptyDesignTimeHtml メソッドをオーバーライドします。
- (省略可能) ControlDesigner から継承された GetErrorDesignTimeHtml メソッドをオーバーライドします。
「Web フォーム テンプレート データ連結コントロール デザイナのサンプル」に、これらのメソッドをオーバーライドする重要なデザイナの例を示しています。
このトピックの最後で定義する単純な ASP.NET サーバー コントロール (Simple
) のための単純なデザイナ (SimpleDesigner
) のサンプル コードを次に示します。Simple
は Text
プロパティを持ち、実行時にはテキスト文字列として表示します。SimpleDesigner
は、単純なタスクを 1 つ、つまり、Simple
の Text
プロパティをデザイン時のハイパーリンクとして格納するタスクを実行し、デザイン時の表示を実行時とは異なる表示にします。
Imports System
Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControls
Imports CustomControls
Namespace CustomControls.Design
Public Class SimpleDesigner
Inherits System.Web.UI.Design.ControlDesigner
Public Overrides Function GetDesignTimeHtml() As String
' Component is the instance of the component or control that
' this designer object is associated with. This property is
' inherited from System.ComponentModel.ComponentDesigner.
Dim simple As Simple = CType(Component, Simple)
If simple.Text.Length > 0 Then
Dim sw As New StringWriter()
Dim tw As New HtmlTextWriter(sw)
Dim placeholderLink As New HyperLink()
' Put simple.Text into the link's Text.
placeholderLink.Text = simple.Text
placeholderLink.href = simple.Text
placeholderLink.RenderControl(tw)
Return sw.ToString()
Else
Return GetEmptyDesignTimeHtml()
End If
End Function
End Class
End Namespace
[C#]
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using CustomControls;
namespace CustomControls.Design {
public class SimpleDesigner : System.Web.UI.Design.ControlDesigner {
public override string GetDesignTimeHtml() {
// Component is the instance of the component or control that
// this designer object is associated with. This property is
// inherited from System.ComponentModel.ComponentDesigner.
Simple simple = (Simple) Component;
if (simple.Text.Length > 0) {
StringWriter sw = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(sw);
HyperLink placeholderLink = new HyperLink();
// Put simple.Text into the link's Text.
placeholderLink.Text = simple.Text;
placeholderLink.href = simple.Text;
placeholderLink.RenderControl(tw);
return sw.ToString();
}
else
return GetEmptyDesignTimeHtml();
}
}
}
次に、Simple
のコードが続きます。DesignerAttribute は太字で示されています。
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Namespace CustomControls
<Designer("CustomControls.Design.SimpleDesigner, CustomControls.Design")> _
Public Class Simple
Inherits WebControl
Private _text As String
Public Property [Text]() As String
Get
Return _text
End Get
Set
_text = value
End Set
End Property
Protected Overrides Sub RenderContents(writer As HtmlTextWriter)
writer.Write([Text])
End Sub
End Class
End Namespace
[C#]
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace CustomControls {
[ Designer("CustomControls.Design.SimpleDesigner, CustomControls.Design") ]
public class Simple : WebControl {
private string text;
public string Text {
get { return text; }
set { text = value; }
}
protected override void RenderContents(HtmlTextWriter writer) {
writer.Write(Text);
}
}
}
参照
Web フォームのデザイン時サポート | Web フォーム テンプレート データ連結コントロール デザイナのサンプル | Web フォーム データ連結コントロール デザイナの実装 | Web フォーム テンプレート エディタの実装