[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 825
  • Last Modified:

How to invoke a Master page button click from a child page

I need to invoke a master page button click from a child page.
I tried tthe following which does not work. I see the alert pop up but the btnInvalidSession is not triggered

CHILD PAGE
If Session(":LoginID:") Is Nothing Then
                Dim btnInvalid As Button = CType(Page.Master.FindControl("btnInvalidSession"), Button)
                Dim val As String = btnInvalid.ClientID
                Dim msgDesc As String = "if (alert('Your session is no longer valid.\n\n Please login again.')) document.getElementById('" & val & "').click();"
                ScriptManager.RegisterClientScriptBlock(Me.Master, Me.Master.GetType(), "Invalid Session", msgDesc, True)
                Exit Sub        
               
            End If

MASTER PAGE - ASPX

<asp:Button ID="btnInvalidSession" runat="server" style="display:none;" OnClick="InvalidSessionButton_Click" />

MASTER PAGE - code behind

Protected Sub InvalidSessionButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInvalidSession.Click
        Try
            Response.Redirect("login.aspx")
        Catch ex As Exception
            ex.ToString()
        End Try

    End Sub

CHILD PAGE
If Session(":LoginID:") Is Nothing Then
                Dim btnInvalid As Button = CType(Page.Master.FindControl("btnInvalidSession"), Button)
                Dim val As String = btnInvalid.ClientID
                Dim msgDesc As String = "if (alert('Your session is no longer valid.\n\n Please login again.')) document.getElementById('" & val & "').click();"
                ScriptManager.RegisterClientScriptBlock(Me.Master, Me.Master.GetType(), "Invalid Session", msgDesc, True)
                Exit Sub        
                
            End If

MASTER PAGE - ASPX

<asp:Button ID="btnInvalidSession" runat="server" style="display:none;" OnClick="InvalidSessionButton_Click" />

MASTER PAGE - code behind

Protected Sub InvalidSessionButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInvalidSession.Click
        Try
            Response.Redirect("login.aspx")
        Catch ex As Exception
            ex.ToString()
        End Try

    End Sub

Open in new window

0
countrymeister
Asked:
countrymeister
1 Solution
 
Alfred A.Commented:
I am presenting excerpts from this link, http://forums.asp.net/p/1127634/1778262.aspx,  check the link for details.

1. In your master page, add a readonly property for your button as follows:

Public ReadOnly Property MasterButton() As Button
        Get
            Return Me.btnYourMasterButton
        End Get
    End Property

2. In your content page add a reference to your master page right after the @Page directive in the aspx code:

<%@ Reference Control="~/YourMasterPage.master"%>

3. In the code behind of your content page, get a reference to your master page, subscribe to your master page button "Click" event and implement the event handler

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler CType(Me.Page.Master, YourMasterPage).MasterButton.Click, AddressOf MasterButton_Click
    End Sub
    Protected Sub MasterButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Write("My Master Button Was Clicked!")
    End Sub

  4. Run the application and click on the button (which is in your master page) and it will show the message (My Master Button Was Clicked!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now