HOw to Raise Events in an ASCX User Control (Is it better in ASPNET 2.0 ?)

Discussion in 'ASP .Net' started by Anonieko, Feb 7, 2006.

  1. Anonieko

    Anonieko Guest

    Anonieko, Feb 7, 2006
    #1
    1. Advertising

  2. Anonieko

    Anonieko Guest

    This is what you have to hand-write



    Raise Events From ASP.NET ASCX User Controls

    Drop a UserControl called MyControl.ascx into a page.

    To add a member variable to the container class, you can simply add the
    line (C#):

    protected MyControl MyControl1;



    STEPS IN RAISING EVENTS FROM ASP.NET ASCX USER CONTROLS.
    ========================================================

    USERCONTROL.ASCX.CS
    -------------------

    1. Define a public member

    public event System.EventHandler TabClicked;



    2. Raise it whenever you want like this:

    if(this.TabClicked != null)
    this.TabClicked(this, new EventArgs());




    PARENTPAGE.ASPX.CS
    ------------------

    1. Drag and Drop the control.

    2. Add in the Page_Load or Init the wire code


    this.MyControl.TabClicked +=new
    EventHandler(MyControl_TabClicked);

    3. Write the Handler

    private void MyControl_TabClicked(object sender, EventArgs e){ /*
    etc... */ }



    If you type "this.MyControl.TabClicked +=" in the IDE, an press TAB
    twice



    PASSING DATA
    ============


    Now, say you wanted to pass some specific data to the container
    control.

    One way is to use a delegate. This comes in really handy, and is in
    fact quite easy too.
    For example, what if you wanted to pass some private value from the
    encapsulated child class?

    1. First create a custom class deriving from System.EventArgs with a
    member variable
    that contains your interesting private value:

    public class TabClickEventArgs : EventArgs
    {
    private String myString;
    public String MyString
    {
    get { return myString; }
    set { myString = value; }
    }
    }

    2. Then create a delegate for your event like so:

    public delegate void TabClickEventHandler(object sender,
    TabClickEventArgs e);


    The rest is just like before, except that you define your custom event
    hander,
    instead of the basic System.EventHandler.

    public event TabClickEventHandler TabClicked;

    And raise it like so.

    // Raise the tabclicked event.
    if(this.TabClicked != null)
    {
    TabClickEventArgs e = new TabClickEventArgs();
    e.SomeString = "test";
    this.TabClicked(this, e);
    }

    Finally, when you wire the event up, your method signature for the
    handler,
    just needs to match the signature of your delegate:

    private void MyControl_TabClicked(object sender, TabClickEventArgs
    e) {}

    -----
     
    Anonieko, Feb 7, 2006
    #2
    1. Advertising

  3. Anonieko

    eblind

    Joined:
    Feb 16, 2010
    Messages:
    1
    Muchas gracias, fue de gran ayuda...
     
    eblind, Feb 16, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jose Walker
    Replies:
    0
    Views:
    723
    Jose Walker
    Jul 27, 2006
  2. Jacol

    raise or not to raise [Newbie]

    Jacol, Feb 3, 2007, in forum: Python
    Replies:
    5
    Views:
    407
    Gabriel Genellina
    Feb 5, 2007
  3. ernest
    Replies:
    2
    Views:
    292
    Roy Smith
    Nov 14, 2010
  4. Jack Bates
    Replies:
    0
    Views:
    279
    Jack Bates
    May 2, 2011
  5. bvdp

    Raise X or Raise X()?

    bvdp, Mar 11, 2012, in forum: Python
    Replies:
    10
    Views:
    370
    Stefan Behnel
    Mar 12, 2012
Loading...

Share This Page