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

x
?
Solved

How to raise masterpage event from dynamic usercontrol?

Posted on 2010-08-23
8
Medium Priority
?
917 Views
Last Modified: 2013-11-07
I have a masterpage that dynamically loads a usercontrol. The masterpage has a public method that raises a public event. I want the usercontrol to call that method to raise the event so the web page can respond to it. Unfortunately, I can't get my usercontrol to recognize the masterpage method. It says my method is not a member of System.Web.UI.MasterPage.

Here's my masterpage code:
Partial Class Master_Mobile
    Inherits System.Web.UI.MasterPage

    Public Event Login()
    Public Sub RaiseLogin()
        RaiseEvent Login()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Create Dynamic Controls
        If ShowLogin And Me.MasterLogin.Controls().Count = 0 Then
            Dim ucLogin As UserControl = LoadControl("~/UserControls/Login.ascx")
            Me.MasterLogin.Controls.Add(ucLogin)
        End If
    End Sub

Open in new window


Here's my usercontrol code:
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Call Page.Validate()

        If Page.IsValid Then
            ' Return to caller
            Call Page.Master.RaiseLogin()     ' THIS LINE FAILS
        End If
    End Sub

Open in new window


What's the correct way to call the public method from this dynamically loaded usercontrol? Thanks in advance.
0
Comment
Question by:ZekeLA
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 33503783
0
 
LVL 1

Author Comment

by:ZekeLA
ID: 33503965
I don't have nested master pages and that link shows how to find a control from a page which is not what I have. Please explain how that applies to my situation.
0
 
LVL 35

Accepted Solution

by:
YZlat earned 1000 total points
ID: 33504248
this part should be helpful in assigning event to your control:

MasterPageFile = "your masterPage";

Control c = (Control)Master.FindControl("Control Id")

c.Event = New EventHandlet(EventsName);

0
Technology Partners: 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!

 
LVL 16

Assisted Solution

by:kris_per
kris_per earned 1000 total points
ID: 33504511

Check out this link => http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx

To see the method of a master page in the content page, you have  to 'strong-type' the master page in content page as following:

<%@ Page  .....
<%@ MasterType  virtualPath="~/MasterPage.master"%>  <= add this line after the Page directive at the top...

now in the cs file of the content page, you can do like:

protected void Page_Load(object sender, EventArgs e)
{
    CompanyName.Text = Master.CompanyName; // CompanyName is a property in master page
}

OR in a button click method
{
    Master.RaiseLogin();
}
0
 
LVL 1

Author Comment

by:ZekeLA
ID: 33504761
kris, I can't strongly type it that way because it's a usercontrol. <%@ MasterType  virtualPath="~/MasterPage.master"%>  doesn't work for the Control directive. I tried <%@ Reference Control="~/Layouts/MyMaster.master"%> but that still doesn't expose the my master page class. When I use the object browser I can't find my master page class in my project's namespace.
0
 
LVL 1

Author Comment

by:ZekeLA
ID: 33504821
Yzlat, I don't understand your suggestion. I don't need to assign the masterpage. it is the usercontrol that can't find the master page's method. Not the page. It makes no sense to search for the control from within the control. Can you be more specific and / or reference the original code I posted?
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33505254

@ MasterType directive can be added only in content page file. i think it is not for controls....below is some code with delegates/events for your code..see if this helps....

In User control class file:

     Delegate Function LoginEvent() As String

     Public Dim LoginEventHandler As LoginEvent

     Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Call Page.Validate()

        If Page.IsValid Then
            ' Return to caller
            ' Call Page.Master.RaiseLogin()     ' THIS LINE FAILS
            LoginEventHandler ( ) ' CALL DELEGATE function
        End If
    End Sub




in masterpage code :

Partial Class Master_Mobile

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Create Dynamic Controls
        If ShowLogin And Me.MasterLogin.Controls().Count = 0 Then
            Dim ucLogin As UserControl = LoadControl("~/UserControls/Login.ascx")
            ucLogin.LoginEventHandler = New LoginEvent(AddressOf Login) ' ADD THIS LINE - TO PASS Login function as delegate
            Me.MasterLogin.Controls.Add(ucLogin)
        End If
    End Sub

    Public Function Login()  As String
        ' this is the function that will get called when submit button is clicked in user control
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:ZekeLA
ID: 33536797
I finally figured out my problem. I didn't include the usercontrol reference on my master page. Once I added

<%@ Register TagPrefix="uc1" TagName="Login" Src="~/UserControls/Login.ascx" %>

I was able to change my master page code as shown below. Thanks to both for your guidance and suggestions.


    Public Event MasterLoggedIn(ByVal sender As Object, ByVal e As EventArgs)

    Protected Sub ProcessLogin(ByVal sender As Object, ByVal e As System.EventArgs)
         RaiseEvent MasterLoggedIn(Me, New EventArgs())
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Create Dynamic Controls
        If ShowLogin Then
            Call AddLogin()
        End If

        ' Content Visibility
        Me.MasterContent.Visible = Not ShowLogin
        Me.MasterLogin.Visible = ShowLogin
    End Sub

    Protected Sub AddLogin()
        Dim uc As MyNamespace.UCLogin = LoadControl("~/UserControls/Login.ascx")
        Me.MasterLogin.Controls.Add(uc)
        AddHandler uc.LoggedIn, AddressOf ProcessLogin
    End Sub

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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