用户控件使用事件与调用页面交互

1.定义事件参数类

using System;

 

namespace ASPNetCookbook.CSExamples

{

/// <summary>

/// This class provides the definition of the custom event arguments used

/// as the event arguments for the message sent from this control. This

/// class simply inherits from System.EventArgs and adds a message property.

/// </summary>

public class MessageEventArgs

{

private String mMessage;

 

    /// <summary>

    /// This property provides the ability to get/set the message in the

    /// event args

    /// </summary>

    public String message

    {

     get

     {

        return (mMessage);

     }

 

     set

     {

     mMessage = value;

     }

    } // message

} // MessageEventArgs

}

2.定义控件代码

Test.ascx

<asp:Button ID="btnSendMessage" runat="server"

               Text="Send Message"

               OnClick="btnSendMessage_Click" />

 

Text.ascx.cs

using System;

 

namespace ASPNetCookbook.CSExamples

{

    /// <summary>

    /// This class provides the code-behind for

    /// CH05UserControlCommSourceCS.ascx

    /// </summary>

    public partial class CH05UserControlCommSourceCS : System.Web.UI.UserControl

    {

      // define the delegate handler signature and the event that will be raised

      // to send the message

      public delegate void customMessageHandler(Object sender,

                                    MessageEventArgs e);

      public event customMessageHandler OnSend;
			

 

 

      ///***********************************************************************

      /// <summary>

      /// This routine provides the event handler for the send message button

      /// click event. It creates a new MessageEventArgs object then raises

      /// an OnSend event

      /// </summary>

      ///

      /// <param name="sender">Set to the sender of the event</param>

      /// <param name="e">Set to the event arguments</param>

      protected void btnSendMessage_Click(object sender,

                                        EventArgs e)

      {

        MessageEventArgs messageArgs = new MessageEventArgs();

        messageArgs.message = "This message came from the source user control";

 

        if (OnSend != null)

        {

          OnSend(this, messageArgs);

        }

     }  // btnSendMessage_Click
			

   }  // CH05UserControlCommSourceCS

}

调用页面调用

 

Test.aspx

 

<asp:lable id="labMessage" runat="server"/>

<table width="60%" align="center" border="0">

          <tr>

            <td class="PageHeading">

              Source User Control:

            </td>

          </tr>

      <tr>

         <td bgcolor="#ffffcc" align="center" height="75">

             <ASPCookbook:SourceControl id="ucSource" runat="server" />
			

         </td>

      </tr>

</table>

 

Test.aspx.cs

 

protected void Page_Load(object sender, EventArgs e)

    {

//在调用页面中挂接控件事件      

ucSource.OnSend +=

  new CH05UserControlCommSourceCS.customMessageHandler(this.updateLabel);

    }

 

private void updateLabel(Object sender,

                    MessageEventArgs e)

    {

    // update the label with the message in the event arguments

    labMessage.Text = e.message;

    }
			

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值