How can i to call a public method of page from user control?

Last post 06-22-2009 3:12 AM by DEMO. 8 replies.

Sort Posts:

  • How can i to call a public method of page from user control?

    09-10-2007, 12:23 PM
    • Member
      point Member
    • Eustaquio
    • Member since 06-18-2002, 1:50 AM
    • Posts 3

    In asp.net 1.1 it´s easy: ((PageNameSpace.PageClass)Page).PublicMethod()

    but in asp.net 2.0 I don´t know to do it.

  • Re: How can i to call a public method of page from user control?

    09-10-2007, 12:41 PM
    • Member
      89 point Member
    • machta
    • Member since 08-23-2007, 8:35 AM
    • Czech Republic
    • Posts 38

    This is calling static method, not public. So you can replace public with static and use NameSpace.Class.Method() to call this method. (For calling non static method you have to use istance of the class.)

    You want to call this method from event?

    (I didn't completely understand your question - I didn't use asp.net 1.1)

     

  • Re: How can i to call a public method of page from user control?

    09-10-2007, 1:00 PM
    • Member
      point Member
    • Eustaquio
    • Member since 06-18-2002, 1:50 AM
    • Posts 3

    Thanks but I need a public or protected method, not static, from event in user control and i have an instance of page, this is page property of user control that is a pointer to container page.

    The problem is that the page property is a Page class and i need to cast to MyPage to access it`s public methods or properties.

     

  • Re: How can i to call a public method of page from user control?

    09-12-2007, 4:19 AM
    Answer

    Hi,

    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>
    /// 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();
        }
    }
    
     


    I hope this helps.

    Thomas Sun
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: How can i to call a public method of page from user control?

    09-12-2007, 5:30 AM
    • Member
      point Member
    • Eustaquio
    • Member since 06-18-2002, 1:50 AM
    • Posts 3

    Great solution. Thanks a lot.

     

  • Re: How can i to call a public method of page from user control?

    07-25-2008, 3:25 AM
    • Contributor
      4,221 point Contributor
    • moredotnet
    • Member since 11-24-2006, 12:07 PM
    • Millenium City
    • Posts 781
    Simply Awesome!
    Vishal Khanna



    ASP.NET|C#|AJAX|SQL|Design Patterns|Interview FAQs
  • Re: How can i to call a public method of page from user control?

    08-07-2008, 3:02 AM

    what if I have different functions to call for different cases like in first form we have BindGrid() method to call and in second class we have BindGrid and BindGridData() to call

  • Re: How can i to call a public method of page from user control?

    08-07-2008, 3:25 AM
    • Member
      96 point Member
    • Leyu
    • Member since 10-08-2007, 3:19 PM
    • Posts 18

    If you are using the UserControl from one page only this is not much of a problem because you can call the Methods of the Page

    by directly casting them to the Page in which you are going to the UserControl

    public partial class UserControl_WebUserControlA : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    	MySpecificPage page = (MySpecificPage)Page;
    	page.BindData();
    	page.BindData2();
    
    	// Any other method defined in the specific page...  
        }
    }
    If your intention is to use the UserControl in multiple pages I favor using the Contracts defined by Interfaces implemented by different classes
    and you can throw NotImplementedException() for some of the methods which a specific Page might not implement or you can defined multiple Interfaces
    of which the UserControl is aware of and do things differently.
    Note that a class can implement multiple interfaces
  • Re: How can i to call a public method of page from user control?

    06-22-2009, 3:12 AM
    • Member
      57 point Member
    • DEMO
    • Member since 07-08-2002, 7:11 AM
    • New York
    • Posts 12
    Hello all.I have a problem when i try to compile i get an error which says that i didn’t t inherits user control .Abstract class i inherits  as you wrote with System.Web.UI.Page . Whan i tryed to change to user control i got error here AbstractClass page = (AbstractClass)Page;int iResult = page.DoSomething();

    so how can i inherits   user control and page base class to Abstract user control

    ~Deepak
Page 1 of 1 (9 items)