Results 1 to 11 of 11

Thread: User controls

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question 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?

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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(thise);
                }
            }
            protected 
    void btnSubmit_Click(object senderSystem.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 senderSystem.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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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?

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    It's really easier than you'd think, check it out:
    StupidControl1.ascx
    VB Code:
    1. <%@ Control Language="vb" AutoEventWireup="false"
    2.     Codebehind="StupidControl1.ascx.vb"
    3.     Inherits="DeathAngelVB.StupidControl1" %>
    4. <asp:TextBox ID="txtName" Runat="server"/>
    5. <asp:Button ID="btnSubmit" Runat="server"
    6.     Text="Submit" OnClick="btnSubmit_Click" />
    StupidControl1.ascx.vb
    VB Code:
    1. Public Class StupidControl1 : Inherits System.Web.UI.UserControl
    2.     Protected txtName As System.Web.UI.WebControls.TextBox
    3.     Protected btnSubmit As System.Web.UI.WebControls.Button
    4.     Public Event SubmitButtonClick As System.EventHandler
    5.     Public Property Name() As String
    6.         Get
    7.             Return txtName.Text
    8.         End Get
    9.         Set(ByVal Value As String)
    10.             txtName.Text = Value
    11.         End Set
    12.     End Property
    13.     Public Sub OnSubmitButtonClick(ByVal e As System.EventArgs)
    14.         RaiseEvent SubmitButtonClick(Me, e)
    15.     End Sub
    16.     Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    17.         OnSubmitButtonClick(New System.EventArgs)
    18.     End Sub
    19. End Class
    StupidControl2.ascx
    VB Code:
    1. <%@ Control Language="vb" AutoEventWireup="false"
    2.     Codebehind="StupidControl2.ascx.vb"
    3.     Inherits="DeathAngelVB.StupidControl2" %>
    4. <br/>
    5. You Entered:<asp:Label ID="lblName" Runat="server"/>
    StupidControl2.ascx.vb
    VB Code:
    1. Public Class StupidControl2 : Inherits System.Web.UI.UserControl
    2.     Protected lblName As System.Web.UI.WebControls.Label
    3.     Public Property Name() As String
    4.         Get
    5.             Return lblName.Text
    6.         End Get
    7.         Set(ByVal Value As String)
    8.             lblName.Text = Value
    9.         End Set
    10.     End Property
    11. End Class
    StupidControlTest.aspx
    VB Code:
    1. <%@ Page Language="vb" AutoEventWireup="false"
    2.     Codebehind="StupidControlTest.aspx.vb"
    3.     Inherits="DeathAngelVB.StupidControlTest"%>
    4. <%@ Register TagPrefix="stupid" TagName="Control1" Src="StupidControl1.ascx" %>
    5. <%@ Register TagPrefix="stupid" TagName="Control2" Src="StupidControl2.ascx" %>
    6. <html>
    7.     <head>
    8.         <title></title>
    9.     </head>
    10.     <body>
    11.         <form runat="server">
    12.             <stupid:Control1 ID="MyStupidControl1" Runat="server"
    13.                 OnSubmitButtonClick="MyStupidControl1_SubmitButtonClick" />
    14.             <stupid:Control2 ID="MyStupidControl2" Runat="server" />
    15.         </form>
    16.     </body>
    17. </html>
    StupidControlTest.aspx.vb
    VB Code:
    1. Public Class StupidControlTest : Inherits System.Web.UI.Page
    2.     Protected MyStupidControl1 As DeathAngelVB.StupidControl1
    3.     Protected MyStupidControl2 As DeathAngelVB.StupidControl2
    4.     Protected Sub MyStupidControl1_SubmitButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
    5.         MyStupidControl2.Name = MyStupidControl1.Name
    6.     End Sub
    7. End Class

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    GREAT, GREAT, GREAT, you are my start Pvb! .
    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!

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Sorry "Star" not "Start"

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question 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.

  10. #10
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    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
  •  



Click Here to Expand Forum to Full Width