Calling a method in parent page from a user control using delegates in ASP.NET

No.of Views2743
Bookmarked0 times
Downloads 
Votes2
By  Prajeesh   On  15 May 2010 21:05:35
Tag : ASP.NET , User Controls
Sometimes you may need to call a method in the parent page from a child user control, for eg:- we have a user control with New, Save, Delete buttons and based on the page type we may need to call appropreate methods.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Sometimes you may need to call a method in the parent page from a child user control, for eg:- we have a user control with New, Save, Delete buttons and based on the page type we may need to call appropreate methods.

Before going to actual aim of this post we must have an idea about delegates and event handlers.

What is a delegate & how should we use it?

A deligate is just like a function pointer in c/c++ , delegates can be used to call a method where the call method can be determined only at run time.

How can we declare, instantiate and call a deligate?

Suppose we have a method to add two numbers and display the result in the webpage.

Step 1 : Declaring a delegate

public delegate void SumOfTheNumbers(int a, int b);

where delegate is a keyword and void is the return type of the delegate.

Step 2: Instantiating and calling the delegate

DisplayNumbersDelegate objDisDelegate = new DisplayNumbersDelegate(AddNumbers);
objDisDelegate(5 ,10 );

where "objDisDelegate" is the delegate variable and "AddNumbers" is the function to be called, note that signature of the AddNumbers function must be similer to the delegate we declared, that is it must accept two integer parameters and returns void, when we instantiate the delegate object we are pointing the AddNumbers function to the delegate variable, we can use delegate variable to call the method by passing the values, here we are calling only one function using this delegate this is called single cast delegate, you can also use delegates to call multiple functions also this type of delegates are called multi cast delegates.

Calling a method in a parent page from a user control :

Suppose we have two text boxes and an add button in the user control and we want to call a method to add two numbers declared in the parent page and display result in the same page.

Step 1: Declare the delegate and event in user control.

public delegate void SumOfTheNumbers(int a, int b);
public event SumOfTheNumbers sumnos;

Step 2:Passing the parmeters to the delegate from click event of the Add button in the user control

protected void btnAdd_Click(object sender, EventArgs e)
 {
   sumnos(int.Parse(txtBoxNum1.Text), int.Parse(txtBoxNum2.Text));
 }

Step 3:Declare the add method in parent page.

public void sumofthenumber(int a, int b)
{
 Response.Output.Write("sum of{0} and {1} is : {3} ",a,b,a+b);
}

Step 4: Instantiate the delegate from the page load event of the parent page.

protected void Page_Load(object sender, EventArgs e)
{
delControl1.sumnos += new TestProject.UserControls.delControl.SumOfTheNumbers(sumofthenumber);
}

and you are done, hope you enjoyed this article.

 
Sign Up to vote for this article
 
About Author
 
Prajeesh
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-India
Joined date-15 May 2010
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
By:Retro JordansDate Of Posted:3/15/2011 9:39:31 PM
We've got viewers methods attached to vogue over web page page prior to,as very well because the we am gday that may assist deposit that a vast majority of a lot of blogvendors go through outlook on-l
We've got viewers methods attached to vogue over web page page prior to,as very well because the we am gday that may assist deposit that a vast majority of a lot of blogvendors go through outlook on-line seeing that contaminents,also the ones you have may possibly vacationers, we would like find out any views,as clearly as a clarify to consider on their own completely satisfied and moreover most most likely will uncover some thing special,and make confident to soundest consideration your enterprise internet logs as
By:IrakliDate Of Posted:10/5/2010 8:17:50 AM
Thanks
Thanks for this post. That I searched for that)
By:Charlie PhillipsDate Of Posted:5/21/2010 2:51:07 PM
Outstanding!
Prajeesh, you're a life-saver!
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top