私は、専用のユーザーコントロールを持つすべての機能を1ページに積ますべての機能から、両極端でのプロジェクトに取り組んできました。少なくとも、私は次のことを行うことをお勧め:
- サイト全体のデザインは、二つ以上のページに表示されます
- 機能は、ユーザーコントロールにカプセル化されなければならないあなたのmasterpagesに属します。たとえば、ログインコントロールはいくつかの場所に表示されることがあります。
- 他の機能は、aspxページに埋め込むことができます。
厄介な部分が通信権を得ています。これは、相互に通信する必要のあるコントロールのレイヤーを深くネストしている場合や、コントロールを通知する必要があるマスターページにコントロールがある場合子ページ(存在する場合)。あなたがコントロールの2つ以上のレベルの間の通信が必要な場合は、次のモデルを使用します。つまり
ProxyNotifier
/ \
Control1 Control2
を、Control1のControl2の両方がProxyNotifierインスタンスへのアクセス権を持っています。 Control1はProxyNotiferを、ProxyNotiferはControl2に信号を渡します。今Control1のControl2のは、彼らがページ上にある関係なく通信できる
public static class ProxyNotifer
{
// NOTE: This delegate should NOT be held in a static variable since
// its session specific
private static Action<string> NotifyEvent
{
get
{
if (!Session.ContainsKey["ProxyNotifyEvent"])
{
Session["ProxyNotifyEvent"] = null;
}
return Session["ProxyNotifyEvent"] as Action<string>;
}
}
public static void Subscribe(Action<string> handler)
{
NotifyEvent += handler;
}
public static void Unsubscribe(Action<string> handler)
{
NotifyEvent -= handler;
}
public static void Invoke(string msg)
{
if (NotifyEvent != null) { NotifyEvent(msg); }
}
}
public class Control1 : UserControl
{
void SomethingHappened()
{
ProxyNotifyer.Invoke("Hello world!");
}
}
public class Control2 : UserControl
{
public Control2()
{
ProxyNotifer.Subscribe(Respond);
}
// ALWAYS unregister your event when your control is disposed,
// otherwise weirdness happens
public void Dispose()
{
ProxyNotifier.Unsubscript(Respond);
base.Dispose();
}
public void Respond(string msg)
{
lblMsg.Text = msg;
}
}
:(注意:未テストコード、本番環境での使用ためのものではありません)
非常に単純な実装は次のようになります。