|
-
Feb 15th, 2004, 06:42 AM
#1
Thread Starter
Frenzied Member
User controls
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?
-
Feb 15th, 2004, 10:37 PM
#2
Hyperactive Member
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
PHP 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" />
StupidControl1.aspx.cs
PHP 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
PHP Code:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="StupidControl2.ascx.cs"
Inherits="DeathAngel.Examples.StupidControl2" %>
<br/>
You Entered:<asp:Label ID="lblName" Runat="server"/>
StupidControl2.ascx.cs
PHP 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
PHP 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>
StupidControlTest.aspx.cs
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;
}
}
}
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.
-
Feb 16th, 2004, 02:17 AM
#3
Thread Starter
Frenzied Member
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?
-
Feb 16th, 2004, 08:16 PM
#4
Hyperactive Member
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.
-
Feb 17th, 2004, 03:06 AM
#5
Thread Starter
Frenzied Member
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
-
Feb 17th, 2004, 08:22 AM
#6
Hyperactive Member
It's really easier than you'd think, check it out:
StupidControl1.ascx
VB 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" />
StupidControl1.ascx.vb
VB 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 Class
StupidControl2.ascx
VB Code:
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="StupidControl2.ascx.vb"
Inherits="DeathAngelVB.StupidControl2" %>
<br/>
You Entered:<asp:Label ID="lblName" Runat="server"/>
StupidControl2.ascx.vb
VB 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 Class
StupidControlTest.aspx
VB 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>
StupidControlTest.aspx.vb
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
-
Feb 18th, 2004, 05:18 AM
#7
Thread Starter
Frenzied Member
-
Feb 18th, 2004, 05:19 AM
#8
Thread Starter
Frenzied Member
Sorry "Star" not "Start"
-
Feb 21st, 2004, 06:58 AM
#9
Thread Starter
Frenzied Member
Subs?
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.
-
Feb 21st, 2004, 11:24 AM
#10
Hyperactive Member
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.
-
Feb 23rd, 2004, 04:12 AM
#11
Thread Starter
Frenzied Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|