Click here to Skip to main content
12,451,232 members (51,785 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
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 12-Oct-12 22:55pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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:

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 13 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100