Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
INFORMATIVE BACKGROUND:
I have a ddl control in master site, i would like to call the 'selectIndexChange event' that sits in master page, and i want to call it in a content page. the reason is because i have an adio.net function that populates a grid in my content page. when i select a new item from my drop down, i dont want the event to be triggered in master page bcs frm inside master page i cant call the adio.net function thats in content page. I thought that i could fix this if i pass the selectIndexChange event from the master to the content page with a delegate. then i would be able to call the function select indexchange and my adio.net code function will be in scope. :)

MY CODE ISSUE:
with trying to set up my delegate function that calls a selectindexevent from mater to content I noticed a redline underneath 'contentCallEvent' it cant be found, it has no reference. Maybe its because my content page is in a panel?
your help would be very much appreciated! thankyou in advance

my code below
:note my content page is in a panel.


MASTERPAGE

C#
protected void ddlSites_SelectedIndexChanged(object sender, EventArgs e)
      {

          if (contentCallEvent != null)
              contentCallEvent(this, EventArgs.Empty);
      }
      public event EventHandler contentCallEvent;


CONTENTPAGE

C#
private void Master_ddlSites_SelectedIndexChanged(object sender, EventArgs e) {
           //from here i should be able to call function and populate grid

           populateGridViewByID_Parent("TEST");
       }


protected void Page_PreInit(object sender, EventArgs e)
       {

           Page.Master.contentCallEvent += new EventHandler(Master_ddlSites_SelectedIndexChanged);
           //error says, masterpage does not contain a reference for contentCallEvent}


What I have tried:

I have tried to find the content page,
C#
ContentPlaceHolder contentPage = Page.Master.FindControl("pnlCollectionsDue") as ContentPlaceHolder;

          contentPage.Page.Master += new EventHandler("ddlSites_SelectedIndexChanged");

i have tried to find the dropdown list
C#
DropDownList ddl = this.Page.Master.FindControl("ddlSites") as DropDownList;
           ddl.SelectedIndexChanged += new EventHandler(Master_ddlSites_SelectedIndexChanged);
Posted
Updated 22-Apr-18 22:56pm

1 solution

You need to tell the content page what the specific type of the master page is before you can use any public properties\methods on that master page. This can be done using the MasterType derective

Working with ASP.NET Master Pages Programmatically[^]

Master page has this dropdown

<asp:DropDownList ID="ddlSites" runat="server" OnSelectedIndexChanged="ddlSites_SelectedIndexChanged" AutoPostBack="true">
    <asp:ListItem Text="One" Value="1" />
    <asp:ListItem Text="Two" Value="2" />
    <asp:ListItem Text="Three" Value="3" />
</asp:DropDownList>


master page code-behind - note I'm using a custom delegate to pass the selected value of the dropdown, you can extend that to pass the text too if needed

public partial class SiteMaster : MasterPage
{
    public delegate void SiteSelectedEvent(string site);

    public event SiteSelectedEvent contentCallEvent;

    protected void ddlSites_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (contentCallEvent != null)
            contentCallEvent(ddlSites.SelectedValue);
    }
}


User control

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="ProjectNamespace.MyControl" %>

<asp:Literal ID="LiteralResult" runat="server" />


User control code behind - the control exposes a method that allows calling code to pass it data

public partial class MyControl : System.Web.UI.UserControl
{
    public void ShowResult (string result)
    {
        LiteralResult.Text = result;
    }
}


Finally the code-behind of the content page. "MyControl1" is the ID of the user control on the page.

protected void Page_Load(object sender, EventArgs e)
{
    Master.contentCallEvent += Master_contentCallEvent;
}

private void Master_contentCallEvent(string site)
{
    MyControl1.ShowResult(site);
}


Also note the content page has the MasterType directive

<%@ MasterType VirtualPath="~/Site.Master" %>


That will allow the change event of the dropdown on the master page to pass the selected data to any event subscribers, and the content page uses that event to pass the data to the user control via a method. Note we try to never directly access controls that belong to other components, instead things are done via methods etc that de-couple the functionality from the implementation.
 
Share this answer
 
v3
Comments
helpwithmycode 23-Apr-18 5:07am    
Was i meant to add this to the masterpage . i did just that and i got a parser error?
Parser Error Message: Circular file references are not allowed.

Line 1: <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="PartnerPortalTest.Site" %>
Line 2: <%@ MasterType VirtualPath="~/Site.Master"%>

sorry man, i just dont know what im doing wrong.
F-ES Sitecore 23-Apr-18 5:11am    
No, you add the MasterType directive to the content page so it knows the type of the master page it sits in. That's what lets it know what methods on the master page are available for it to call.
helpwithmycode 23-Apr-18 5:21am    
I tried that, and code wont compile because of this reason 'UserControl' Does not contain a definition for 'Master'!?

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GCCollectionsdue.ascx.cs" Inherits="Partner_Portal.UserControl.Grid.WebUserControl1"%>
<%@ MasterType VirtualPath="~/Site.Master"%>

thanks again!
F-ES Sitecore 23-Apr-18 5:32am    
That's a user control, not a content page? I thought the code in question was on the content page, where does the user control come into things?
helpwithmycode 23-Apr-18 5:36am    
Ag sorry i forgot to mention, that was important. My content page has a 'user control'. and inside my usercontrol is where i would like to call the function. So its another level deep.

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