Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to handle custom Events?

Status
Not open for further replies.

LochDhu

Technical User
Jul 12, 2001
76
0
0
US
Members,

Default.aspx has an HTML Button that opens Page2.aspx in a new frame, I need the user to set some params and then have Default.aspx post back and do something with the params. I am using window.opener to get Default.aspx to postback. The problem is creating my own Events to capture. I *think* I need to create a class that inherits from Control - I won't have a visual appearance to my control, I just need to define my own Events with my own EventArgs - and I need to learn how to specify which one of my events to handle on PostBack.

Are their tutorials on this? I have a few books but they do not go into details on this. I'm sure this has been done before.

Any comments greatly appreciated.

Scott
 
Scott how you making it on this? I though the post interesting so I wrote a stub to envoke my own event with it's own EventArgs. i'd be interested how you solved your issue. This uses a button click event to fire the control which will envoke the event.
BoulderBum or anyone else any thoughts appreciated.
Marty
The control.
Code:
using System;
using System.Web;	
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ControlLibrary1
{
	/// <summary>
	/// Summary description for MessageControl.
	/// </summary>
	public class MessageControl : Control, INamingContainer
	{

		Label lblSendMessage;

		public delegate void SendMessageEventHandler(object sender, MessageEventArgs e);

		public event SendMessageEventHandler SendMessage;

		public void OnSetAndSendMessage(Object sender, EventArgs e)
		{
			lblSendMessage.Text = "You pressed the button which fires the event.";
			MessageEventArgs messageArgs = new MessageEventArgs();
			messageArgs.Message = "This message is from the SendMessage Event in the Control that has just been fired.";
			messageArgs.ShowMessage = true;
			//envoke our EventHandler
			SendMessage(this, messageArgs);
		}

		public void Page_Load(object src, EventArgs e)
		{
			EnsureChildControls();
		}

		protected override void CreateChildControls()
		{
			Button btnSend = new Button();
			btnSend.Text = "Fire Event";
			btnSend.Click += new EventHandler(this.OnSetAndSendMessage);
			Controls.Add(btnSend);
			Controls.Add(new LiteralControl("<br/>"));
			lblSendMessage = new Label();
			Controls.Add(lblSendMessage);

		}	
	}
//	This is a class derived from EventArgs that should be encapsulated but we left it 
//  in here as this is a proof of concept.
//  The EventArgs that are used in the Custom Event SendMessage
	public class MessageEventArgs : System.EventArgs
	{
		private string sMessage;
		public String Message
		{
			get { return(sMessage);	}
			set	{ sMessage = value; }
		}  
		
		private bool bShowMessage;
		public bool ShowMessage
		{
			get { return(bShowMessage);	}
			set	{ bShowMessage = value; }
		}  
	}
}
The aspx.cs that uses the control
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CSTestWebForms
{
	/// <summary>
	/// Summary description for TestMessageEventArgs.
	/// </summary>
	public class TestMessageEventArgs : System.Web.UI.Page
	{
		protected ControlLibrary1.MessageControl MyCtrl;
		protected System.Web.UI.WebControls.Label Label1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		public void MyCtrl_OnSendMessage(object sender, ControlLibrary1.MessageEventArgs e)
		{
			if (e.ShowMessage)
			{
				Label1.Text = e.Message;
			}
			else
			{
				Label1.Text = "Control did not send a message";
			}
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
			MyCtrl.SendMessage += new ControlLibrary1.MessageControl.SendMessageEventHandler(this.MyCtrl_OnSendMessage);
		}
		#endregion
	}
}
the aspx
Code:
<%@ Register TagPrefix="cc1" Namespace="ControlLibrary1" Assembly="ControlLibrary1" %>
<%@ Page language="c#" Codebehind="TestMessageEventArgs.aspx.cs" AutoEventWireup="false" Inherits="CSTestWebForms.TestMessageEventArgs" %>
<HTML>
	<HEAD>
	</HEAD>
	<body>
		<form id="Form1" runat="server">
			<CC1:MESSAGECONTROL id="MyCtrl" runat="server"></CC1:MESSAGECONTROL>
			<br>
			<asp:label id="Label1" runat="server">Label1</asp:label>
		</form>
	</body>
</HTML>
 
Thanks for the sample code. I have made some progress and actually got it to work. Here is the code for my Control:
Code:
Imports System.ComponentModel
Imports System.Web.UI
Imports System
Imports System.Web.UI.Control


<ToolboxData("<{0}:DWSDGeocode runat=server></{0}:DWSDGeocode>")> Public Class DWSDGeocode
    Inherits System.Web.UI.Control
    Implements IPostBackEventHandler
    Implements INamingContainer

    Public Event MapLocation As EventHandler

    Protected Overridable Sub OnMapLocation(ByVal e As GeocodeEventArgs)
        RaiseEvent MapLocation(Me, e)
    End Sub

    Public Overridable Sub RaisePostBackEvent(ByVal eventArgument As String) _
      Implements IPostBackEventHandler.RaisePostBackEvent
        OnMapLocation(New GeocodeEventArgs(eventArgument))
    End Sub



    Public Class GeocodeEventArgs
        Inherits EventArgs
        Private _Coordinates As String
        Private _XCoordinate As Double
        Private _YCoordinate As Double

        Public GeocodeEventArgs()

        Public Property Coordinates() As String
            Get
                Return _Coordinates
            End Get
            Set(ByVal Value As String)
                _Coordinates = Value.ToString
            End Set
        End Property

        Public Property XCoordinate() As Double
            Get
                Return _XCoordinate
            End Get
            Set(ByVal Value As Double)
                _XCoordinate = Value
            End Set
        End Property

        Public Property YCoordinate() As Double
            Get
                Return _YCoordinate
            End Get
            Set(ByVal Value As Double)
                _YCoordinate = Value
            End Set
        End Property

        Public Sub New(ByVal Coords As String)
            _Coordinates = Coords
            'ParseCoordinates()
        End Sub

        Private Sub ParseCoordinates()
            Dim split As String() = Nothing
            split = _Coordinates.Split(",")
            _XCoordinate = CType(split(0), Double)
            _YCoordinate = CType(split(1), Double)
        End Sub

    End Class

End Class

I'm still trying to learn all the caveats of this, I didn't realize how much work it was.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top