Web Site Techniques - 2

Visual Studio Web Site Tricks and Techniques - Part 2
ASP.Net tutorial by Barbie Hocking  ©2009
Previous Page
B.  Using a recursive search to locate controls 
An asp.net page is composed of multiple controls. The parent page is the primary container control which encompasses all controls residing on the page. This includes master pages and user controls which in turn are containers for their underlying controls. The FindControlRecursive method detailed below walks through a container control to locate a specific control id. If it locates a container control, it recursively calls itself which allows it to locate
all controls within all container controls.
1.  FindControlRecursive parameters:

- Control root: the container from 
   which the recursive search
   should begin. 

- string id: the id of the control to
  locate
public Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id)
    {
         return root;
    }
    foreach (Control c in root.Controls)
    {
        Control ctrl = FindControlRecursive(c, id);
        if (ctrl != null)
        {
            return ctrl;
        }
    }
    return null;
}
 

C.  Bubbling events 
A user control is unable to directly access a public method on the parent page (See Interface Techniques for methods of doing this utilizing an interface).  A user control can bubble an event to the parent page using the following approach:
 
1.  a. In a user control, enter the code
    within the
    "#region Bubble Event" region
 
b. Enter "OnBubbleClick(e);" 
    within an event
//User Control Processing
#region Bubble Event
public event EventHandler BubbleClick;

protected void OnBubbleClick(EventArgs e)
{
    if (BubbleClick != null)
    {
        BubbleClick(this, e);
    }
}
#endregion Bubble Event

protected void btnDisplayOnParent_Click(object sender, EventArgs e)
{
    OnBubbleClick(e);
}
 
2.  a. In the parent's "Page_Load" 
    method, create an event handler
    for the BubbleClick event

b. Create the BubbleClick event, i.e.
    ParentPage_BubbleClick.
    This event will be asynchronously
    executed when the child calls
    OnBubbleClick(e)

//Parent Processing
protected void Page_Load(object sender, EventArgs e)
{
    //Create bubble event
    this.ucFirstChild.BubbleClick += new EventHandler(ParentPage_BubbleClick);
}

private void ParentPage_BubbleClick(object sender, EventArgs e)
{
    //Do event processing
}
 

D.  Session variables and the Master Page
A master page often needs to display text values determined by a content page. Session variable can be used for this purpose.  There is a timing consideration concerning the sequence of asp.net events merging a master page with content pages.  The master page Init and Page_Load events occur before the content page is processed. This means the content page initializes the session variable after the master page Page_Load event.

To work around this, add a PreRender event to the master page. PreRender occurs after content processing  completes.  The session variable will have been initialized by the content pages when the master page PreRender event runs.
 
1. 

a. Add an event handler for PreRender
    in the Page_Load.

b. Create the MasterPage_PreRender event
    The session variable can be safely
    processed here.

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.PreRender +=new EventHandler(MasterPage_PreRender);
    }

    void MasterPage_PreRender(object sender, EventArgs e)
    {
        if (Session["tempBubbledEvent"] != null)
        {
            this.lblBubbledEvent.Text = Session["tempBubbledEvent"].ToString();
            Session["tempBubbledEvent"] = null;
        }
    }
}

 
Previous Page
 Contact Us     Links      ©2010 GeekPhilosopher.com - All rights reserved
Powered by www.ezjooz.com