2

Referencing: ASP.Net : How to call a Master page event handler from Content page event handler?

Issue: SiteAssessment.btnNext_Click(sender, e) is not accessable because it's protected, but it's not that I can see??!!

Okay so Top of aspx.vb is

Imports System.Data.SqlClient


Partial Class Assessment

    Inherits System.Web.UI.Page

    <%@ MasterType VirtualPath="~/SiteAssessment.master" %>

    Public Sub btnNext_click(sender As Object, e As System.EventArgs)

         SiteAssessment.btnNext_Click(sender, e)

    End Sub

Master Page (SiteAssessment.master.vb)

Partial Class SiteAssessment

    Inherits System.Web.UI.MasterPage


    Public Sub btnNext_Click(sender As Object, e As EventArgs)
        'Do Stuff Here

        If Session.Item("questionbtn") Is Nothing Then
            Session.Item("questionbtn") = 1
        Else
            Session.Item("questionbtn") = Session.Item("questionbtn").ToString + 1
        End If

        'Call PrcchkListAnswers()

        'Call PrcQuestionText()

        Dim lblSession As Label = DirectCast(MainContent.FindControl("lblSession"), Label)
        lblSession.Text = Session.Item("questionbtn").ToString

    End Sub
Community
  • 1
  • 1
indofraiser
  • 1,014
  • 3
  • 18
  • 50

1 Answers1

2

First, you'll need to put the MasterType tag at the top of your .aspx markup page (not the .vb code-behind).

Secondly, reference the Master Page using the Page.Master Property. You'll then be able to access your method:

Me.Master.btnNext_Click(sender, e)
NoAlias
  • 9,218
  • 2
  • 27
  • 46
  • Okay so I have it working but it picks up both buttons in Protect Sub Page Load... Call btnNext_clickassessment(sender, e) Call btnPrev_clickassessment(sender, e) then at the top of the page (still aspx.vb) Protected Sub btnNext_clickassessment(sender As Object, e As System.EventArgs) Me.Master.btnNext_Click(sender, e) End Sub Protected Sub btnPrev_clickassessment(sender As Object, e As System.EventArgs) Me.Master.btnPrev_Click(sender, e) End Sub then... – indofraiser Aug 27 '15 at 13:51
  • Public Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click 'Do Stuff Here If Session.Item("questionbtn") Is Nothing Then Session.Item("questionbtn") = -1 Else Session.Item("questionbtn") = -1 End If End Sub and the btnNext_Click on the Master.vb page – indofraiser Aug 27 '15 at 13:52
  • I also already have <%@ MasterType VirtualPath="SiteAssessment.Master" %> on the .aspx page – indofraiser Aug 27 '15 at 14:03
  • Can you please explain the problem a little better without including code in the comments please? – NoAlias Aug 27 '15 at 14:07
  • 2 buttons in Master Page, user clicks on one of the buttons. I need to know which button was pressed before executing the code on the content page. – indofraiser Aug 27 '15 at 14:08
  • Really that should be a different question, but CType(sender, Button).ID will get you the ID of the button clicked. – NoAlias Aug 27 '15 at 14:11
  • Thanks.. I'm obviously missing something... I have tried your code in the page load, I'm guess that's the wrong place? Also should it be Dim btn As String = Ctype... ? (Head is full from hours of trying different things!) – indofraiser Aug 27 '15 at 14:22
  • Do "If CType(sender, Button).ID = "SomeId" Then" in the Click Event of the button to determine the ID of the button that was clicked. If you need it further downstream (in the content page): Instead of calling a button click, call some new method where you can pass an ID. – NoAlias Aug 27 '15 at 14:35
  • Thanks again - I think I need a walk though as I am totally lost. Tried the Click Event in a few ways, just a bit shattered or confused now :-( – indofraiser Aug 27 '15 at 14:56
  • Well if my answer solved the issue with calling a sub on the MasterPage from the Content page, please mark it as the accepted answer. I'd advise asking another question about your new issue and the community will help to solve it. – NoAlias Aug 27 '15 at 15:00
  • This did not work for me until I did a DirectCast. I needed to call a function called MyFct in my Master Page. My Master Page was named: Master.Master. So I did DirectCast(Me.Master, Master).MyFct(). Now Master is the class name for Master.Master obviously. In C# this would have been ((Master)Me.Master).MyFct(); – John Foll Jul 07 '23 at 22:09