Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

Calling Method in Parent Page from User Control

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
4 Oct 2010CPOL1 min read 67.7K   7   7
Calling method in parent page from user control

In ASP.NET, we develop custom user control as a reusable server control independent of any containing parent aspx page. User control has its own public properties, methods, delegates, etc. that can be used by parent aspx page. When a user control is embedded or loaded into a page, the page can access public properties, methods, delegates, etc. that are in user control. After loading the user control, there a situation may arise like calling methods in page itself. But when a user control is developed, it has no knowledge of containing page. So it becomes a trick to call the page method.

In .NET, Delegate class has one method DynamicInvoke. DynamicInvoke method is used to invoke (late-bound) method referenced by delegate. We can use this method to call a method in parent page from user control. Let’s try with this example.

First, create a user control called CustomUserCtrl. Its code will look something like this:

C#
public partial class CustomUserCtrl : System.Web.UI.UserControl
{
private System.Delegate _delWithParam;
public Delegate PageMethodWithParamRef
{
set { _delWithParam = value; }
}

private System.Delegate _delNoParam;
public Delegate PageMethodWithNoParamRef
{
set { _delNoParam = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
}

protected void BtnMethodWithParam_Click(object sender, System.EventArgs e)
{
//Parameter to a method is being made ready
object[] obj = new object[1];
obj[0] = "Parameter Value" as object;
_delWithParam.DynamicInvoke(obj);
}

protected void BtnMethowWithoutParam_Click(object sender, System.EventArgs e)
{
//Invoke a method with no parameter
_delNoParam.DynamicInvoke();
}
}

Then add this user control into an aspx page. The code behind of this page is as:

C#
public partial class _Default : System.Web.UI.Page
{
delegate void DelMethodWithParam(string strParam);
delegate void DelMethodWithoutParam();
protected void Page_Load(object sender, EventArgs e)
{
DelMethodWithParam delParam = new DelMethodWithParam(MethodWithParam);
//Set method reference to a user control delegate
this.UserCtrl.PageMethodWithParamRef = delParam;
DelMethodWithoutParam delNoParam = new DelMethodWithoutParam(MethodWithNoParam);
//Set method reference to a user control delegate
this.UserCtrl.PageMethodWithNoParamRef = delNoParam;
}

private void MethodWithParam(string strParam)
{
Response.Write("<br/>It has parameter: " + strParam);
}

private void MethodWithNoParam()
{
Response.Write("<br/>It has no parameter.");
}
}

BtnMethodWithParam and BtnMethowWithoutParam are two different buttons on the user control that are invoking the methods in the parent page. On Page_Load of the page, we are setting the references of page class methods to delegate type properties in the user control. Click different buttons of user control, you will see MethodWithParam(string strParam) and MethodWithNoParam() methods called.

This is all we have to do to call page class methods from user control in ASP.NET.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Imfinity India Pte Ltd, Noida (Excelsoft Company)
India India
http://www.imfinity.com/

Comments and Discussions

 
QuestionNested user control Pin
Member 1332048120-Jul-17 9:49
Member 1332048120-Jul-17 9:49 
Questioncode give me an error pls help Pin
barikinil18-Feb-13 12:03
barikinil18-Feb-13 12:03 
AnswerRe: code give me an error pls help Pin
Dinesh K Mandal18-Feb-13 18:34
professionalDinesh K Mandal18-Feb-13 18:34 
GeneralMy vote of 5 Pin
Narud Shiro16-Feb-12 6:15
Narud Shiro16-Feb-12 6:15 
Questionthanks! Pin
bencohen9-Jul-11 15:33
bencohen9-Jul-11 15:33 
Generalpersistence delegate - does not work Pin
dacardona19-Mar-11 11:33
dacardona19-Mar-11 11:33 
AnswerRe: persistence delegate - does not work Pin
Marcelo Lujan [El Bebe.Net ]29-Nov-12 12:13
Marcelo Lujan [El Bebe.Net ]29-Nov-12 12:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.