Results 1 to 2 of 2

Thread: Content Page Accessing Event Handlers from Two Different Master Pages

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2013
    Posts
    1

    Content Page Accessing Event Handlers from Two Different Master Pages

    I have a ASP.NET content page where the master page is set dynamically. Both master pages contain a print button and an associated event handler for the click event of the button. Unfortunately I receive a run-time error when I attempt to dynamically access the event from the master page that was not initially assigned.

    Below are code snippets:

    Page_Load():
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim strPageTitle As String = "Evaluator Data Form - Instructions"

    If Page.MasterPageFile.Contains("masEvaluators.master") Then
    Dim mp As Evaluators_masEvaluators = CType(Me.Master, Evaluators_masEvaluators)
    AddHandler mp.SaveEDF, AddressOf imgbtnPrint_Click

    ElseIf Page.MasterPageFile.Contains("masStaff.master") Then
    Dim mp As Staff_maStaff = CType(Me.Master, Staff_maStaff)
    CType(Page.Master.FindControl("imgbtnPrint"), ImageButton).Visible = True

    AddHandler mp.SaveEDF, AddressOf imgbtnPrint_Click
    End If

    which produces the following error when I load the page and reference the "masStaff.master" master page:

    System.InvalidCastException: Unable to cast object of type 'ASP.staff_masstaff_master' to type 'Evaluators_masEvaluators'. at Evaluators_EDF_Instructions.Page_Load....

    I can solve the problem by adding a print button to the content page, but that defeats the purpose of using a master page.

    Is there a solution to this problem? I have spent many hours researching this issue on the web, but have not found a solution as of yet.

    Thanks!

  2. #2
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Content Page Accessing Event Handlers from Two Different Master Pages

    I'm not on my dev machine so can't check/give some code that I think addresses this but from memory the solution is.

    1. create a base class that derives from master page that your 2 master pages inherit from.
    2. In that base class define your print method... ? > not certain how your wire up events will work but maybe this is a different approach.
    3. In your @ page directives use <%@ MasterType TypeName="BaseMaster" %> so you refrence the master page by type not individual class as showen in the following msdn link.

    http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx

    This should prevent the cast error because it's 1 class not 2

    Hope that helps.
    Last edited by brin351; Jul 8th, 2013 at 05:48 AM. Reason: forgot link
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width