Alec Tang

Professional Web Developer/ Web Designer

I build websites based on the latest web standards providing the best possible solution to your company

  • Home
  • About
  • Portfolio
  • Contact
  • Blog
Subscribe Feed

How to call parent and get data from usercontrol

Posted by Alec on Tue, 16 Jun 2009, in ASP.NET C#   

UserControls are great, particularly in making the code more eye friendly, seperating big bunch of codes into different parts and be able to re-use them throughout the application. However, there are times when we need to reference something in the parent page or control from the usercontrol level. That's when I had an issue today, and finally solved it using the brilliant delegate.

Here's how it work:

Suppose I have a parent page that has these:
1. UserControl
2. Label

The UserControl itself is a web form with a number of fields. Now, what I want to do is whenever someone fills in the form (usercontrol), the result will get sent back to the parent and displayed in the label. With the use of delegate, this can be done easily:

In the usercontrol, simply create a delegate:

public delegate void FormSubmittedHandler(object sender, EventArgs e);

This has to be outside the class.

Then, also create an event handler:


        public event FormSubmittedHandler Changed;

        protected virtual void FormSubmitted(object sender, EventArgs e)
        {
            this.Title= txbTitle.text;
            if (Changed != null)
                Changed(this, e);
        }

This event handler will update the usercontrol's properties and then publish to its subscribers (which is the parent). We could call this event handler whenever we want, for example when the submit button is clicked or ontextchanged if you are using Ajax.

In the parent, which is the subscriber, we need to fetch the data and do something:

         protected void Page_Init(object sender, EventArgs e)
        {
            usercontrolWebForm.Changed += new FormSubmittedHandler (updateLabel);
        }

        protected void updateLabel(object sender, EventArgs e)
       {
            this.txbLabel = usercontrolWebForm.Title;
        }

How nice and easy that is?
 

Comments Be the first to write a comment. Comment gets approved before publishing.

Post Your Comment

 
 
   

Search

 

Latest Posts

  • List Attachments Open as Read Only
  • Website finally back up from Google's block
  • Sys.WebForms.PageRequestManagerParserErrorException
  • How Google treats Content Duplication
  • How to create HTML column in Sharepoint List View
  • How Sharepoint stores User Data
  • How to send email via Sharepoint
  • Malaysia Airlines launched iPhone Application: MHMobile
  • Intranet, the next big market
  • How to retrieve and update from a multi choice Checkboxlist ...

Categories

  • Browsers (1)
  • Projects (1)
  • Web Design (7)
  • Sharepoint (21)
  • Telerik (5)
  • Wordpress (1)
  • Internet (2)
  • SQL (5)
  • LINQ (3)
  • ASP.NET C# (34)
  • JavaScripts (3)
  • IIS (0)
  • Industry (1)
  • Tools (8)
  • SEO (5)

Archives

  • September 2010 (1)
  • August 2010 (1)
  • May 2010 (1)
  • April 2010 (3)
  • March 2010 (5)
  • February 2010 (4)
  • January 2010 (11)
  • November 2009 (3)
  • October 2009 (1)
  • September 2009 (9)
  • August 2009 (3)
  • July 2009 (4)
  • June 2009 (1)
  • May 2009 (2)
  • April 2009 (8)
  • March 2009 (6)
  • February 2009 (2)
© Copyright 2009 Alec Tang. All Rights Reserved.
This site is conform to W3C Standard XHTML & CSS