Web Site Techniques









Visual Studio Web Site Tricks and Techniques
ASP.Net tutorial by Barbie Hocking  ©2010
Next Page           
The Web Application Vs Web Site Model article highlighted differences between the two Visual Studio web models.
A Web Site's master pages, web forms, and user controls are not stand-alone classes. Because of this, there's caveats on how master, parent, and children can communicate with each other. A user control's public methods and properties are accessible from its parent, but the user control cannot directly access its parent 's public methods and properties.

Several cross-communication tricks and techniques are available.  The sample code for this tutorial performs the following:
  1. Master Page accessing a child user control property
  2. Web form parent accessing a child user control method
  3. Web form parent recursively searching for child user control server controls
  4. Web form parent dynamically loading a child user control
  5. Child user control recursively searching for parent web form server controls
  6. Child user control bubbling event to parent web form
  7. Passing Session values to a master page
C# Sample Code
Sample Code Page


An additional technique not covered in this lesson is the use of Interfaces.  An interface provides full cross-pollination of public properties and methods between master pages, web forms, and user controls. Refer to the Interface Techniques tutorial for to learn how to use this method. 

Sample Code Overview
A.  Accessing a child user control public method or property 
A user control becomes publicly accessible when it is a made into a class.  This is done by assigning it a ClassName in the @Control directives. Once a user control is a class object, a reference to it can be created with the @Reference directive within the page or control that needs to utilize it. This reference provides the ability to:

  • Access to the user controls public methods and properties 
  • Dynamically load the user control
1.  a. Create a user control.

b. Add a ClassName to the @ Control
    directive. This name must be
    identical to the name in the .cs
    file.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FirstChild.ascx.cs"
         Inherits="UserControls_FirstChild" ClassName="UserControls_FirstChild" %>

*****************************************************************************************************
public partial class UserControls_FirstChild
{
}
 
2.  Create the user control public methods or properties
public void PostParentMsg(string msgChild)
{
    this.lblMsgFromParent1.Text = msgChild;
}

public string CHILD1_MSG { get { return this.txtDisplayOnParent.Text; } }
 
3.  Register the control on a parent. The parent can be a master page, web form, or another user control.
<%@ Register Src="UserControls/FirstChild.ascx" TagName="FirstChild" TagPrefix="uc1" %>
 
4.  If the user control was dragged and dropped to the parent control, this entry was automatically created and the control resides within the parent.
<uc1:FirstChild ID="ucFirstChild" runat="server" />
 
5.  If the user control needs to be dynamically created, add the control to the parent with this code.
a. Create a placeholder 


b. Dynamically load the control
<asp:PlaceHolder ID="phFirstChild" runat="server"></asp:PlaceHolder>


UserControls_FirstChild ucFirstChild = (UserControls_FirstChild)Page.LoadControl
                                                                       ("~/UserControls/FirstChild.ascx");
phFirstChild.Controls.Add(ucFirstChild);
 
6.  Access the public method or property from the parent.
ucFirstChild.PostParentMsg(this.txtDisplayOnChild1.Text);

this.lblChild1Msg.Text = ucFirstChild.CHILD1_MSG;
 
Next Page         
 Contact Us     Links      ©2010 GeekPhilosopher.com - All rights reserved
Powered by www.ezjooz.com