Sunday, January 9, 2011

Accessing variables on and from masterpages, pages and usercontrols in asp.net

I was working on a project and had to perform certain actions like

-Access variables and functions on another page from an event generated from the MasterPage .
-Accessing variables on another page from a userControl .
-Accessing variables on the MasterPage .

The challenge i had here was that i had never done all these before .I have read a couple of books, blog , read source code from open source applications but never seen a situation like that .Then i asked myself the question.
Why would one want to carry out any of the procedures i stated above ?
I had to guess that they are some reasons to do this is ----
-It means that the design of the program was wrong from the beginning .
-You are forced to use it to fulfill a certain condition .
-It is the last choice you got.
Anyway i solved the problems by carrying out the above three procedures and i think it might help someone out there .Here is how i did it

Access variables and functions on another page from an event generated from the MasterPage
Create a function to generate the event on the MasterPage and expose the variable to access using properties {get;set}
Write no code on the event .You might want to if you need it.
protected void CheckOut_Click(object sender, EventArgs e)
    {
         
    }

Create a reference to the MasterPage from the child page
<%@ MasterType VirtualPath="~/MyMasterPage.master" %>


Add and Init-Event on the secondary page .On that Init_Page event get the reference to the MasterPage and generate an event handler delegate that will point to a function in the secondary page .
protected void Page_Init(object sender, EventArgs e)
{
        Master.CheckOutBut.Click += new EventHandler(bnDoSomethingIn_Click);

}

Function to call on the child page
protected void bnDoSomethingIn_Click(object sender, EventArgs e)
 {
        Master.CheckOutBut.Click += new EventHandler(bnDoSomethingIn_Click);

 }

-Accessing variables on another page from a userControl .
Create an abstract class and let it inherit from the page class and some abstract methods that are signatures to the methods you have to override on the child page
public abstract class AbstractPage:System.Web.UI.Page
{
 public AbstractPage()
 {
  //
  // TODO: Add constructor logic here
  //
 }
// some methods
    public abstract void DoSomething();
     public abstract void MyMethodTwo();

}
Let the page inherit from abstractPage class you created
public partial class MyPage : AbstractPage
{
    public void override DoSomething()
     {  
         ----
         ------
     }
}

Access a function on the host(.aspx) page from the UserControl
private void SomeFunction()
    {
        AbstractPage MyPage = (AbstractPage)Page; // reference to  hostPage
        MyPage.DoSomething();
    }


-Accessing variables on the MasterPage .
Expose the variables on the MasterPage
public LinkButton CheckOutBut
    {
        get { return checkOutBut; }
    }

    public Label NameLabel
    {
        get { return lblName; }
    }

Create a reference to the MasterPage from the child page
// Add this on the top of the child Page(.aspx) under the page definitions
<%@ MasterType VirtualPath="~/MyMasterPage.master" %>

//from the userControl you can access the MasterPage variables using
 MyMasterPage mp = (MyMasterPage)Page.Master;
 mp.CheckOutBut

//from the childPage you can access the MasterPage variables using
 Master.NameLabel.Text = "Something" ;

Happy Programming .