Solved

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

Posted on 2010-11-16
1
806 Views
Last Modified: 2012-06-21
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
Comment
Question by:countrymeister
1 Comment
 
LVL 21

Accepted Solution

by:
Alfred1 earned 500 total points
ID: 34151647
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question