Interview Questions forum : Ask your doubt
 
 
Please login or register to take complete advantage of this forum
September 05, 2008, 09:23:11 PM
635 Posts in 231 Topics by 812 Members
Latest Member: abadri.06
Mywalkin Forum  |  Interview Preparation  |  Microsoft Technologies Interview Questions  |  How can i call a public method of page from user control? « previous next »
Pages: [1]
Author Topic: How can i call a public method of page from user control?  (Read 258 times)
mamuni
Newbie
*
Posts: 27


View Profile Email
« on: July 03, 2008, 07:51:30 AM »

    How can i  call a public method of page from user control?
« Last Edit: August 07, 2008, 01:04:54 PM by mamuni » Logged
PratapCh
Newbie
*
Posts: 3


View Profile Email
« Reply #1 on: July 08, 2008, 02:52:34 AM »

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.
Smiley
Logged
Pages: [1]
Mywalkin Forum  |  Interview Preparation  |  Microsoft Technologies Interview Questions  |  How can i call a public method of page from user control? « previous next »
    Jump to:  


    Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC | Sitemap