Is it possible to raise an event from a user control to a different user control? Or is it only possible to raise events from a user control to a web form?
Printable View
Is it possible to raise an event from a user control to a different user control? Or is it only possible to raise events from a user control to a web form?
hmmm...it's not really possible to raise an event to anything as far as i know. Your control raises events and anyone listening responds to them. So if one control needs to take action when another control fires an event, you could have the container handle the control event and handle the other control at that point. Here's a stupid example(literally, this is a stupid example):
StupidControl1.ascx
StupidControl1.aspx.csPHP Code:<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="StupidControl1.ascx.cs"
Inherits="DeathAngel.Examples.StupidControl1" %>
<asp:TextBox ID="txtName" Runat="server"/>
<asp:Button ID="btnSubmit" Runat="server"
Text="Submit" OnClick="btnSubmit_Click" />
StupidControl2.ascxPHP Code:namespace DeathAngel.Examples
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class StupidControl1 : System.Web.UI.UserControl
{
protected TextBox txtName;
protected Button btnSubmit;
public event System.EventHandler SubmitButtonClick;
public string Name
{
get
{
return txtName.Text;
}
}
public void OnSubmitButtonClick(System.EventArgs e)
{
if (SubmitButtonClick != null)
{
SubmitButtonClick(this, e);
}
}
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
OnSubmitButtonClick(new System.EventArgs());
}
}
}
StupidControl2.ascx.csPHP Code:<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="StupidControl2.ascx.cs"
Inherits="DeathAngel.Examples.StupidControl2" %>
<br/>
You Entered:<asp:Label ID="lblName" Runat="server"/>
StupidControlTest.aspxPHP Code:namespace DeathAngel.Examples
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class StupidControl2 : System.Web.UI.UserControl
{
protected Label lblName;
public string Name
{
get
{
return lblName.Text;
}
set
{
lblName.Text = value;
}
}
}
}
StupidControlTest.aspx.csPHP Code:<%@ Page language="c#" Codebehind="StupidControlTest.aspx.cs"
AutoEventWireup="false" Inherits="DeathAngel.Examples.StupidControlTest" %>
<%@ Register TagPrefix="stupid" TagName="Control1" Src="StupidControl1.ascx" %>
<%@ Register TagPrefix="stupid" TagName="Control2" Src="StupidControl2.ascx" %>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
<stupid:Control1 ID="MyStupidControl1" Runat="server"
OnSubmitButtonClick="MyStupidControl1_SubmitButtonClick" />
<stupid:Control2 ID="MyStupidControl2" Runat="server" />
</form>
</body>
</html>
This ended up being an extremely and annoyingly verbose example, but the point was that one control raised an event, the container caught it and dispatched the appropriate action to the other control. Anywho, hopefully that made a little sense.PHP 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 DeathAngel.Examples
{
public class StupidControlTest : System.Web.UI.Page
{
protected StupidControl1 MyStupidControl1;
protected StupidControl2 MyStupidControl2;
protected void MyStupidControl1_SubmitButtonClick(object sender, System.EventArgs e)
{
MyStupidControl2.Name = MyStupidControl1.Name;
}
}
}
Thanks, the only problem I now have, is that I use Visual Basic indstead of C# :( . But as I read your code, I understand that it is possible to raise an event from one control to another?
sorry bout that, i noticed your other post showing vb but i'd already posted this one. in answer to your question, no, it's not possible to raise an event to a specified listener. you raise an event and other things can choose to listen for those events and take action or do nothing at all. that code just shows how you can set it up so that one control can be affected by events of another control. but if you can see in the code, neither control knows about the other one.
Well this is also exactly what I need. If you have the code in VB I would be very glad if you post it ;)Quote:
that code just shows how you can set it up so that one control can be affected by events of another control. but if you can see in the code, neither control knows about the other one.
It's really easier than you'd think, check it out:
StupidControl1.ascxStupidControl1.ascx.vbVB Code:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="StupidControl1.ascx.vb" Inherits="DeathAngelVB.StupidControl1" %> <asp:TextBox ID="txtName" Runat="server"/> <asp:Button ID="btnSubmit" Runat="server" Text="Submit" OnClick="btnSubmit_Click" />StupidControl2.ascxVB Code:
Public Class StupidControl1 : Inherits System.Web.UI.UserControl Protected txtName As System.Web.UI.WebControls.TextBox Protected btnSubmit As System.Web.UI.WebControls.Button Public Event SubmitButtonClick As System.EventHandler Public Property Name() As String Get Return txtName.Text End Get Set(ByVal Value As String) txtName.Text = Value End Set End Property Public Sub OnSubmitButtonClick(ByVal e As System.EventArgs) RaiseEvent SubmitButtonClick(Me, e) End Sub Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) OnSubmitButtonClick(New System.EventArgs) End Sub End ClassStupidControl2.ascx.vbVB Code:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="StupidControl2.ascx.vb" Inherits="DeathAngelVB.StupidControl2" %> <br/> You Entered:<asp:Label ID="lblName" Runat="server"/>StupidControlTest.aspxVB Code:
Public Class StupidControl2 : Inherits System.Web.UI.UserControl Protected lblName As System.Web.UI.WebControls.Label Public Property Name() As String Get Return lblName.Text End Get Set(ByVal Value As String) lblName.Text = Value End Set End Property End ClassStupidControlTest.aspx.vbVB Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="StupidControlTest.aspx.vb" Inherits="DeathAngelVB.StupidControlTest"%> <%@ Register TagPrefix="stupid" TagName="Control1" Src="StupidControl1.ascx" %> <%@ Register TagPrefix="stupid" TagName="Control2" Src="StupidControl2.ascx" %> <html> <head> <title></title> </head> <body> <form runat="server"> <stupid:Control1 ID="MyStupidControl1" Runat="server" OnSubmitButtonClick="MyStupidControl1_SubmitButtonClick" /> <stupid:Control2 ID="MyStupidControl2" Runat="server" /> </form> </body> </html>VB Code:
Public Class StupidControlTest : Inherits System.Web.UI.Page Protected MyStupidControl1 As DeathAngelVB.StupidControl1 Protected MyStupidControl2 As DeathAngelVB.StupidControl2 Protected Sub MyStupidControl1_SubmitButtonClick(ByVal sender As Object, ByVal e As System.EventArgs) MyStupidControl2.Name = MyStupidControl1.Name End Sub End Class
GREAT, GREAT, GREAT, you are my start Pvb! :cool: :cool: :cool: .
That was exactly the code, that I needed.
I am amazed how fast the web apps become when I only use web controls. It's much faster than even Java Applets!
Sorry "Star" not "Start" :bigyello:
Hi again Pvb, what if I wish to run a sub on usercontrol2 when I click a button on usercontrol1? The sub only affects members on usercontrol2.
You'd raise a click event from the button click in usercontrol1. In the page, catch the click event that was raised from usercontrol1 in a sub(that's your eventhandler). In that sub, dispatch whatever you want to do to usercontrol2. Make sure the sub in usercontrol2 is public. I assume you'll have an instance variable(page level) for usercontrol2 in your page class, you'll use that to call the sub in usercontrol2. And actually, the answer is in the previously posted code. Only diff is that instead of exposing a property on usercontrol2 you expose a method(sub).
From your questions, it seems like those controls are pretty tightly coupled. Is it expected that both of your usercontrols that need to talk to each other are going to be used separately in other parts of your application(ie does each control depend on the other to function properly)? Might be worth considering combining the functionality of both usercontrols into one usercontrol, if that makes sense to your app, it may not.
Thanks for your reply, I think the best thing to do is to raise the sub from the same handler. I stiil need two different controls, because control1 contains information which is used in 26 other controls (e.g. control 2). Thanks for your advice.