Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HOw to Raise Events in an ASCX User Control (Is it better in ASPNET 2.0 ?)

Reply
Thread Tools

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

 
 
Anonieko
Guest
Posts: n/a
 
      02-07-2006
I know Visual Studio lacked support on easily writing code
to raise events from a ascx user control ( because you
have to hand write them).
(http://codebetter.com/blogs/brendan..../06/27795.aspx)


Question: Is it better and easier now in ASPNET 2.0 , meaning VS 2005?

 
Reply With Quote
 
 
 
 
Anonieko
Guest
Posts: n/a
 
      02-07-2006
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) {}

-----

 
Reply With Quote
 
 
 
 
eblind eblind is offline
Junior Member
Join Date: Feb 2010
Posts: 1
 
      02-16-2010
Muchas gracias, fue de gran ayuda...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Raise X or Raise X()? bvdp Python 10 03-12-2012 04:08 PM
"raise (type, value, traceback)" and "raise type, value, traceback" Jack Bates Python 0 05-02-2011 05:23 PM
raise Exception or raise Exception() ernest Python 2 11-14-2010 08:14 PM
raise or not to raise [Newbie] Jacol Python 5 02-05-2007 11:46 PM
User Control (ASCX) do not raise post back on ASP.NET 2 Jose Walker ASP .Net 0 07-27-2006 02:36 PM



Advertisments