Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Web Development 6 22
Header Font Size in Grid View 6 24
Making Table Thru ASP Response.write 5 19
[form-control] Retain values after a POST action 21 33
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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…

756 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