How to call page method in UserControl?
//Delegate and Event
//Page
Method2: Customize Page Base class.
public partial class MyUser : System.Web.UI.UserControl
{
public delegate void CallDelegate();
public event CallDelegate Callhandler;
protected void Page_Load(object sender, EventArgs e)
{
}
public void OnHandler()
{
if (Callhandler != null)
{
Callhandler();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
OnHandler();
}
}
{
public delegate void CallDelegate();
public event CallDelegate Callhandler;
protected void Page_Load(object sender, EventArgs e)
{
}
public void OnHandler()
{
if (Callhandler != null)
{
Callhandler();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
OnHandler();
}
}
//Page
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyUser1.Callhandler += new MyUser.CallDelegate(GetHello);
}
public void GetHello()
{
Response.Write("Hello");
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
MyUser1.Callhandler += new MyUser.CallDelegate(GetHello);
}
public void GetHello()
{
Response.Write("Hello");
}
}
Method2: Customize Page Base class.
/// <summary>
/// Summary description for AbstractBaseClass
/// </summary>
public abstract class AbstractBaseClass : System.Web.UI.Page
{
public AbstractBaseClass()
{
//
// TODO: Add constructor logic here
//
}
public abstract int DoSomething();
}
In the Page, we need to inherit this base class and override the abstract method declared in the base class:
public partial class UserControl_Default : AbstractBaseClass
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override int DoSomething()
{
return 1 + 1;
}
}
In the UserControl, We just need to call the method which exists in the base class:
public partial class UserControl_WebUserControlA : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
AbstractBaseClass page = (AbstractBaseClass)Page;
int iResult = page.DoSomething();
}
}
/// Summary description for AbstractBaseClass
/// </summary>
public abstract class AbstractBaseClass : System.Web.UI.Page
{
public AbstractBaseClass()
{
//
// TODO: Add constructor logic here
//
}
public abstract int DoSomething();
}
In the Page, we need to inherit this base class and override the abstract method declared in the base class:
public partial class UserControl_Default : AbstractBaseClass
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override int DoSomething()
{
return 1 + 1;
}
}
In the UserControl, We just need to call the method which exists in the base class:
public partial class UserControl_WebUserControlA : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
AbstractBaseClass page = (AbstractBaseClass)Page;
int iResult = page.DoSomething();
}
}