Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Capture MasterPage dynamic usercontrol event from page

Posted on 2010-08-23
6
Medium Priority
?
797 Views
Last Modified: 2013-11-07
My masterpage has a method which loads a usercontrol dynamically when a user clicks an imagebutton on the page. When the user clicks a button in the usercontrol, I need to raise an event which is processed by the calling page. I could have the masterpage raise the event since it knows about the usercontrol.

But I wonder if there's a way to have the user control raise the event and have the content page listen directly.

Here's a summary of the sequence of events:
user clicks image button on page
page calls masterpage method
masterpage adds usercontrol
user clicks button on user control
user control raises event or user control raises masterpage event
page captures usercontrol event or page captures masterpage event

Please let me know if it's possible to have the page capture the user control event directly.

Thanks.

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
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:MatrixDweller
ID: 33503703
You could put the user control inside an iFrame so that the masterpage does not get called.
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33503780

See if the below psuedo-code which mainly uses delegate/event helps...


class UserControl1
{
    // usercontrol defines ButtonClickedEvent
    private delegate void ButtonClickedEvent();

    public ButtonClickedEvent ButtonClickedEventHandler;

   private button1_OnClick(...)
   {
      //•user clicks button on user control
        // when button is clicked on usercontrol
        // it calls the delegate
        if(ButtonClickedEventHandler != null)
               ButtonClickedEventHandler();
}

>•page calls masterpage method
in page code-behind:
{
    // when calling master page method
    // pass delegate for the handler method whic is in Page code-behind..
    // here OnButtonClicked is the method of the Page
    // which will get called when usercontrol button is clicked
    masterPage.CallMethod(new ButtonClickedEventHandler(OnButtonClicked));
}

public void OnButtonClicked()
    {
        // got the event; process it
     }

> •masterpage adds usercontrol
in master page code-behind:
public CallMethod(ButtonClickedEventHandler handler)
{
    // this method is called by Page
    // and delegate is a param
    // set the delegate object in usercontrol's delegate property
    > •masterpage adds usercontrol here  
    userControl1.ButtonClickedEventHandler += handler;
}

0
 
LVL 1

Author Comment

by:ZekeLA
ID: 33504124
1. Are you able to translate into VB.NET. I tried an online converter but it didn't work and I think VB.Net doesn't require the delegates.

I think my problem isn't the actual coding but my project. For some reason I can't see the public method of my master page. If you could look at my other post (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26423038.html), it contains the code I'm having problems with.

When I look at my web site's object viewer, it shows my master page class under App_Web_np1n6_lb but NOT under D\...\MyWebSite.
0
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 
LVL 16

Expert Comment

by:kris_per
ID: 33504525

This link =>  http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx
shows how to access the members of a master page in the content page...see if this helps you call the master page method....
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33505394

Below is vb equivalent pseudo-code for the above delegate code...note to call the method of master page from the content page...@MasterPage directive should have been set in the content page as shown in the link above....

In User control class file:
    
    // usercontrol defines ButtonClickedEvent
    Delegate Function ButtonClickedEvent() As String

    Public Dim ButtonClickedEventHandler As ButtonClickedEvent

   Protected Sub button1_Click(.......

        //•user clicks button on user control
        // when button is clicked on usercontrol
        // it calls the delegate
        LoginEventHandler ( ) ' CALL DELEGTE method

    End Sub


in content page class file:
>•page calls masterpage method

    // when calling master page method
    // pass delegate for the handler method whic is in Page code-behind..
    // here OnButtonClicked is the method of the Page
    // which will get called when usercontrol button is clicked
    masterPage.CallMethod(New ButtonClickedEvent(AddressOf  OnButtonClicked));

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





> •masterpage adds usercontrol
in master page code-behind:
Public Function CallMethod( handler As ButtonClickedEvent)

    // this method is called by Page
    // and delegate is a param
    // set the delegate object in usercontrol's delegate property
    > •masterpage adds usercontrol here  

    userControl1.ButtonClickedEventHandler = handler;

End Funcion

Open in new window

0
 
LVL 1

Accepted Solution

by:
ZekeLA earned 0 total points
ID: 33536829
Problem I had was in not referencing the usercontrol on the masterpage. Once that was done, I could refer to it's methods / events.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

688 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