Code snippets and programming help

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

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.

Search

About Prajeesh

I am a web developer specialized in ASP.net, i have hands-on experience in ASP.net, SQL Server and javascript, I holds a degree in Masters of Computer Science and Bachelors degree in Information Technology.
Feel free to contact me for any queries or consulting services.
email:prajeeshkkindia[at]gmail.com