Solved

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

Posted on 2010-11-16
1
801 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now