[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to raise events in c#

Posted on 2006-05-06
12
Medium Priority
?
14,467 Views
Last Modified: 2010-05-18
im very curious to learn how to raise events in c# with delegates and it will be nice if you show me step by step how to create events and how to utilze delegates in this example

just a side note: im trying to raise events from the previous page but i could not able to do that

i'm using vs 2005 asp.net 2 (C#)

thanks.
0
Comment
Question by:nisarkhan
  • 3
  • 3
  • 3
9 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16623410
The 'previous page' no longer exists in memory.  So you can't raise an event from the 'previous' page.

AW
0
 
LVL 1

Expert Comment

by:Chrisbzd
ID: 16623725
To fire an event:

//1st declare the event and event handler:
public event EditHomesHandler EditHomes;
public delegate void EditHomesHandler(object sender, EditMoviesEventArgs e);
public virtual void OnEditHomes(EditMoviesEventArgs e) {
    if (EditHomes != null)
            EditHomes(this, e);
    }

//2nd call the event from
protected void SomethingClicked(object o, EventArgs e){
     this.OnEditHomes(new EventArgs());
}

To catch the above event: lets say that the above event was declared in a user control of type AdminHomeControl. On a page we have declared a control of that type (protected AdminHomeControl ctlHome). Just add an event handler to the page for that control like you do for buttons.

protected void Page_Load(object o, EventArgs e){
         //add an event listener
         ctlHome.EditHomes += new EditHomesHandler(EditHomesHandler);
}


protected void EditHomesHandler (object o, EventArgs e){
        //some code here to handle the event
}



Another really helpful thing is to sometimes pass data along with the event instead of the plain EventArgs. To do this you need to create a seperate class that inherits from EventArgs. You then can create properties and members for the class and use it instead of EventArgs in the above example.
0
 
LVL 1

Expert Comment

by:Chrisbzd
ID: 16623732
Please Note.......... that EditMoviesEventArgs in the delegate declaration and in OnEditHomes should be EventArgs. EditMoviesEventArgs is a class that inherits from EventArgs. I'm using it to pass some data with the event.

public event EditHomesHandler EditHomes;
public delegate void EditHomesHandler(object sender, EditMoviesEventArgs e);
public virtual void OnEditHomes(EditMoviesEventArgs e) {
    if (EditHomes != null)
            EditHomes(this, e);
    }
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Author Comment

by:nisarkhan
ID: 16625750
thanks for the response

is there a way i can use something like this?
i have two aspx pages

1) a.aspx
2) b.aspx

i want to use a.aspx button event in b.aspx page
is that possible?

here is the code im trying to write but i dont see the event.

a.aspx:

public ImageButton ImageButtonAddVisit
{
   get { return imgbtnAddvisit; }
}
 

b.aspx:

protected void Page_Init(object sender, System.EventArgs e)
{
a.  <<<i dont see the ImagebuttonAddvisit property here?
}

i'm missing anything?

thanks
0
 
LVL 1

Expert Comment

by:Chrisbzd
ID: 16626380
Your not going to be able to throw an event from one page to another. You can only throw events from controls (buttons, custom controls, dropdownlists, etc....) and catch them on the page they are on or with other controls. You can't use events between pages because if your user is at page A, page B doesn't exist. If you want to pass data do another page you can use a form POST, pass variables on the query string, or use the Session object to store the value, so it can be used on the next page.

To pass a string value to page b.aspx from page a.aspx using a form, you could do something like this:
<form method="POST" action="b.aspx">
      <input type="text" id="txtName" name="txtName" />
</form>

then on b.aspx page load you can use the Request object to get the posted value. (I beleive its something like Request.Forms["txtName"].

Or if you passed the name on the querystring from a.aspx like http://localhost/b.aspx?name=testing
you could get the value of the name variable in the query string with Request["name"].
0
 
LVL 7

Author Comment

by:nisarkhan
ID: 16626465
Chrisbzd :

i understand pretty well passing the data between pages but i was curious to know if that is possible to pass events between pages,

what about master.page to regular pages can you raise events?
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16629604
"what about master.page to regular pages can you raise events?"  NO, not there either.  Master.page simply acts as a 'template'  - sort of like a Base class in an inheritance heirarchy.  The master.page provides the basic structure that is then extended when the 'regular' page is created, using the master.page as the base.  Master pages do not exist as a separate entity.

AW
0
 
LVL 7

Author Comment

by:nisarkhan
ID: 16635348
hmm....

Arthur, if i understood correctly what you saying:

let says, i have a master page and in that master page , i have top header and in that top header i have "save button" and i have 15 contentplaceholder pages and when i click on the "save button" how to do i capture the text from content page?

i belive you have to raise events correct?

0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 1000 total points
ID: 16635620
the master page simply defines the LOOK of the implemented pages.  You will need to provide the code, for each page.  Having a Master page simply relieves you from the work of setting up each and every page so that they look  the same.  It also provides a mechanism for any code that MIGHT be in common.  But the specific code for each page is still you responsibility, unless you want each and every page to behave precisley the same (which would be sort of 'dumb')

You will be providing the code to respond to the Button_Click event raised for each page.  You are not raising that event yourself, in your code.

AW
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month18 days, 18 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question