How to handle master page button event in content page?
Asked Answered
O

3

7

There's more than question and article about the same exact question but I have a couple more related questions and was hoping to get some answers.

  1. I've heard of two approaches to find the button and add the handler or use an interface (Check both approaches from here) .. Which one do you suggest ?

  2. If you could please illustrate the 'Interface' option with some code and where to class the interface file cause it's not readable in the page when I try to inherit it!

Ourself answered 16/4, 2011 at 14:25 Comment(0)
N
8

Second aproach is IMO better. The first choice couples a page to the specific master page, and it is not nice.

All files are placed in the same folder.

IPageInterface.cs:

namespace CallFromMasterPage
{
    public interface IPageInterface
    {
        void DoSomeAction();
    }
}

Default.aspx.cs:

namespace CallFromMasterPage
{
    public partial class Default : System.Web.UI.Page, IPageInterface
    {
        public void DoSomeAction()
        {
            throw new NotImplementedException();
        }
    }
}

Site.Master.cs:

namespace CallFromMasterPage
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
            IPageInterface pageInterface = Page as IPageInterface;
            if (pageInterface != null)
            {
                pageInterface.DoSomeAction();
            }
        }
    }
}

There are other approaches. E.g. you can publish an event via event broker.

Nutritive answered 16/4, 2011 at 14:58 Comment(4)
Ok that's great, but I have a problem .. what is the right place to put the interface file .. I'm using an N-Tier architecture PL, BLL, DAL ? I'm really stuck in this detail as my page code behind file won't see my interface even if I placed it in the same folder or even tried to add an App_Data folder!Ourself
Do you use web site or web application project?Nutritive
A follow up question, I hope you don't mind .. Your code works like a charm but I don't understand this part "IPageInterface pageInterface = Page as IPageInterface;". Could you break it up a little please?Ourself
Mine is coming up as NULL hence doing a postback :/Pitchblack
W
0

See From My Openion it will be best if you make use of event handlers...and even with the custom delegate..

like this

public delegate ReturnType MasterPageButtonHandler(CustomEventArgs ObjPriargs);
public event MasterPageButtonHandler MasterPagebuttonClick;
.
.
.
.
Button.click+=new EventHandler(Button1_Click);
.
.
.
protected void Button1_Click(Object sender,EventArgs e)
{
     if(MasterPagebuttonClick!=null)
     {
         CustomEventArgs ObjPriargs=new CustomEventArgs();
         ObjPriargs.Property1=SomeValu1;
         ObjPriargs.Property2=SomeValu2;
         MasterPagebuttonClick.Invoke(ObjPriargs);
     }
}
.
.
.
public class CustomEventArgs
{
      Public DataType Property1{get;set;}
      Public DataType Property2{get;set;}
      Public DataType Property3{get;set;}
}
.
.
.
//    Now in your aspx Page
MyMaster m=Page.Master as MyMaster;
m.MasterPagebuttonClick+=new MasterPageButtonHandler(MasterPageHandler_Click);
.
.
.
protected void MasterPageHandler_Click(CustomEventArgs ObjPriargs)
{
    //You code/////
}

going through this manner give some flexibility like in case if in future you want to pass some data tour content page when clicked..its easily possible.

Wreath answered 16/4, 2011 at 15:52 Comment(1)
I really appreciate your concert .. but are you advertising for your blog :S:D!!?Ourself
T
0

I have a calendar in my MasterPage, but I need to use this date to bind a datagrid in a ContentPage.

MasterPage html:

<asp:Calendar ID="calAgenda" DefaultView="Days" Format="dd/MM/yyyy" runat="server" BackColor="White" CssClass="templeteCalendar" Visible="false" OnSelectionChanged="calAgenda_SelectionChanged"></asp:Calendar>

ContentPage html:

<asp:TextBox runat="server" ID="txtFechaBusqueda" CssClass="form-control templeteLabel" MaxLength="10" />

MasterPage C# Code:

protected void calAgenda_SelectionChanged(object sender, EventArgs e)
{
    TextBox txtCalendarDate = (TextBox)ContentPlaceHolder1.FindControl("txtFechaBusqueda");
    txtCalendarDate.Text = calAgenda.SelectedDate.ToString().Substring(0, 10);
}

Here We have use a EventHandler in the ContentPage to catch the event click in the MaserPage.

ContentPage C# Code:

Calendar btnCalendar = Master.FindControl("calAgenda") as Calendar;
btnCalendar.SelectionChanged += new EventHandler(btnCalendar_Click);

Then We need to define the btnCalendar_Click function.

protected void btnCalendar_Click(object sender, EventArgs e)
{
    DoSomething();
}
Tjaden answered 10/5, 2022 at 20:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.