5

PageDefault.aspx に UserControl1.ascx と UserControl2.ascx の 2 つの UserControls があります。

イベントバブリングを使用してメソッド ( GetLabelText() in UserControl1.ascx)を呼び出すにはどうすればよいですか?UserControl2.ascx

これは私のコード例です。ボタン ( ) をクリックすると、イベント バブリングを使用しUserControl2Button1 in UserControl1.ascxてメソッドを呼び出します。GetLabelText() from UserControl2.ascx

PageDefault.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageDefault.aspx.cs" Inherits="TelerikAjaxEvents.PageDefault" %>
<%@ Register TagPrefix="uc" TagName="UserControl1" Src="~/UserControl1.ascx" %>
<%@ Register TagPrefix="uc" TagName="UserControl2" Src="~/UserControl2.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page Default</title>
</head>
<body>
    <form id="form1" runat="server">
        UserControl1:
        <uc:UserControl1 ID="UserControl1" runat="server" />

        UserControl2:
        <uc:UserControl2 ID="UserControl2" runat="server" />

    </form>
</body>
</html>

UserControl1.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs" Inherits="TelerikAjaxEvents.UserControl1" %>
<asp:Label ID="UserControl1Label1" runat="server"></asp:Label>

UserControl1.ascx.cs

public partial class UserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void GetLabelText() 
        {
            UserControl1Label1.Text = "Text is Visible";
        }
    }

UserControl2.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl2.ascx.cs" Inherits="TelerikAjaxEvents.UserControl2" %>
<asp:Button ID="UserControl2Button1" runat="server" Text="Send" 
    onclick="UserControl2Button1_Click" />

UserControl2.ascx.cs

public partial class UserControl2 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UserControl2Button1_Click(object sender, EventArgs e)
        {
            //Call method from UserControl1 (GetLabelText()) - Show Label text - USING BUBBLE EVENT
        }
    }
4

3 に答える 3

5

これには多くの方法があります。マークの答えは、System.Web.UI.Control ベース コントロールの組み込み機能部分を使用して、従来はイベント バブリングとして知られていたことを示唆しています。ただし、独自のイベントを公開し、それにバインドし、コントロール階層を介してイベントをバブルアップするのは簡単なことです。ASP.NET での BubbleEvent の使用の詳細については、以下をお読みください。これらの MSDN 記事はどちらも .NET 1.1 向けに書かれたものであることに注意してください。

イベントのバブリング

イベント バブリング コントロールのサンプル

K. Scott Allen は、「イベント バブリング」の実装がどのように見えるかを正確に示すために、彼の投稿「 ASP.NET (C#) での Web ユーザー コントロールからのイベント バブリング」で良い仕事をしています。インスピレーションについては、例の次の変更を参照してください。

GetLabelText() を使用した UserControl1

public partial class UserControl1 : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void GetLabelText() 
    {
        UserControl1Label1.Text = "Text is Visible";
    }
}

発信者がサブスクライブできる公開された BubbleClick イベント ハンドラーを持つ UserControl2。

public partial class UserControl2 : System.Web.UI.UserControl
{
    protected Button UserControl2Button1;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public event EventHandler BubbleClick;

    protected void OnBubbleClick(EventArgs e)
    {
        if(BubbleClick != null)
        {
            BubbleClick(this, e);
        }
    }      
    protected void UserControl2Button1_Click(object sender, EventArgs e)
    {
        // do some stuff
        OnBubbleClick(e);
    }
}

PageDefault は UserControl2 の BubbleClick イベント ハンドラーをサブスクライブし、UserControl1.GetLabelText() を実行します。

public partial class PageDefault : WebPage
{
    UserControl1 userControl1;
    UserControl2 userControl2;

    protected void Page_Load(object sender, EventArgs e)
    {
        UserControl2.BubbleClick += RootBubbleClickHandler;
    }

    protected void RootBubbleClickHandler(object sender, EventArgs e)
    {
        if (sender is UserControl2)
        {
            // subscribe UserControl1 to UserControl2 click event and do something
            userControl1.GetLabelText();
        }

        // check for other controls with "BubbleClicks"         
    }
}
于 2012-06-30T15:33:54.303 に答える
4

イベント バブリングは、ASP.NET WebForms ではあまり理解されていない概念です。それは存在します (そしてほとんどのデータ バインドされたコントロールで使用されます) が、「ユーザー コントロールにイベントを実装する」という単純な概念と誤解されることがよくあります (K Scott Allen のように)。

イベント バブリングの核心は、イベントが処理されるかルートに到達するまで、コントロール階層を上に移動することです。これにより、ハンドラー コードの集中化が可能になります。Control.RaiseBubbleEvent および Control.OnBubbleEvent メソッドを使用して実装されます。主な使用例は、多くの子コントロールを持つコントロールです (Repeater、ListViews などを考えてください)。個々のコントロールをサブスクライブする代わりに、Repeater はその OnBubbleEvent でそれらすべてをキャッチし、それらに対して 1 つの ItemCommandEvent を発生させます。

(K. Scott の例とは対照的に) 本当にイベント バブリングを使用したい場合は、次のようになります。

class Page {
    override bool OnBubbleEvent(object sender, EventArgs e) {
         var btn = sender as Button;
         if (btn == null) return false;

         // You may want to do further checking that the source is what you want
         // You could use CommandEventArgs to do this
         this.uc1.GetLabelText();
         return true;
    }
}

イベントのシーケンスは次のようになります。

- Button Clicked
- Button RaiseBubbleEvent
- UserControl OnBubbleEvent returns false (default, since you didn't override)
- Page OnBubbleEvent
于 2012-06-30T17:34:32.723 に答える
0

PageDefault.aspx で UserControl1 をパブリック プロパティ ("UserControl1" など) として宣言してから、UserControl2 で を使用できParent.Page.UserControl1.GetLabelText()ますか?

于 2012-06-30T15:19:40.617 に答える