ACCESS PARENT PAGE METHOD FROM USER CONTROL

ACCESS PARENT PAGE METHOD FROM USER CONTROL

Since the user control can access controls of parent page very easily but in order to access the methods, delegates will be required with its DynamicInvoke method.

We can then call DynamicInvoke method in order to access the method in parent page.

 

Declaring property in User Control as below:

 

public partial class UserControl1 : System.Web.UI.UserControl

{

private System.Delegate _del;

public Delegate DelTest

{

set { _del = value; }

}

}

Bind the delegate with method of Parent Page:

 

public partial class ParentPage : System.Web.UI.Page

{

delegate void Del1();

protected void Page_Load(object sender, EventArgs e)

{

Del1 del = new Del1(fun1);

this.usrControlId.DelTest = del;

}

private void fun1(){

// content here

}

}

Call the method in User Control as:

public partial class UserControl1 : System.Web.UI.UserControl

{

protected void btn_Click(object sender, EventArgs e)

{

_delTest.DynamicInvoke()

}

}

CONCLUSION:

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

Tags:
  • ACCESS PARENT PAGE METHOD,
  • USER CONTROL,
  • Leave a Reply