Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-16
1
Medium Priority
?
821 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 21

Accepted Solution

by:
Alfred A. earned 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Suggested Courses

610 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