User controls
How we can invoke user controls public method from within the page?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Hemant KumarRank #1,202 · 1.5K reputationPosted Oct 18, 2011, 9:35 PM EDT
Based on my understanding, you want to know how to call the method that is declared on the page from UserControl. If I have misunderstood you, please feel free to let me know.
We can call the method that is declared on the page from UserControl in the asp.net 2.0. For example, we can create an abstract base class for the Page in the App_Code folder. The following is the abstract base class which will be inherited by the Page:
/// /// Summary description for AbstractBaseClass /// 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(); } }