Click here to Skip to main content
15,891,744 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to access function from content page to master page?mns i have button on master page and when i will click on button then functionality will change according to content page.
Posted

You can do that in a few ways.
Set delegate on Master Page.

C#
public partial class Site : System.Web.UI.MasterPage
{
    public Action SomeAction;
}

public partial class Page : System.Web.UI.Page
{
    protected void btn_Click(object sender, EventArgs e)
    {
         ((Site)Master).SomeAction= PageAction;
    }

    public void PageAction(){
       
    }
}


Create your custom event:

C#
public delegate void SomeEventHandler
            (object source, SomeEventArgs args);

    public class SomeEventArgs 
    {
        public object Data { get; set; }
    }

public partial class Site : System.Web.UI.MasterPage
{
    public event SomeEventHandler SomeEvent;
}


public partial class Page : System.Web.UI.Page
{
    protected void btn_Click(object sender, EventArgs e)
    {
         ((Site)Master).SomeEvent= PageHandler;
    }

    public void PageHandler(object source, SomeEventArgs args){
       args.Data = 1;
    }
}


It's usefull for sharing some simple data between page and master.
But I would only recomend this if you hurry.
It's not cleanest way to do it IMHO.

I would prefer to create some context data class. Register it in IoC container with life span HttpSession. Dependency Injection would take care of setting
correct instance of this context in your Page and MasterPage and you could share some data between them using your context as intermediate object.

But it is more complex but solution and I don't know if you even using container in your project.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900