AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=943&pId=-1
Control Tree Recursion Using ASP.NET 2.0
page
by Tom Blanchard
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 30811/ 42

Introduction

Some people may have already read Steve Orr's Article on control tree recursion, and may be wondering what differences there may be when using the .NET Framework 2.0. When using the .NET Framework 2.0, one may throw all kinds of monkey wrenches into the situation.

One of these would be using a "Master Page". When using a "Master Page", you will only see one control, which will be the master page that your current page is inheriting. This creates a problem, when trying to access a control that lies within the page itself. When creating a page without utilizing a master page, you will also find it difficult to find your controls, if you loop through the "Page.Controls" collection. This particular issue has an easy fix. The fix is to reference the Form within the page and loop through its controls.

Listing 1

Control oFrm = Page.Form;
//loop through panel controls
foreach (Control oCtl in oFrm.Controls)
  {
   //find your control and do with it as you like here
   .....
  }
Creating your Master Page

So now let's create a page that has a "Master Page" and get the controls within it.

Firstly, create a new solution. Then Create a "Master Page". In the "Master Page", create a table with two content panels. We are going to create a page that would have links on the left and content displayed within panels on the right.

Listing 2

<table cellspacing="0" cellpadding="0" style="width:100%;vertical-align:top">
        <tr>
            <td id="LeftNav" style="vertical-align:top">
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
            </td>
            <td id="Content">
          <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
        </asp:ContentPlaceHolder>  
            </td>
        </tr>
    </table>
Creating Your WebForm

Ok, now add a new "Web Content Form", and select the "Master Page" that you just created. In your "Web Content Form", you should see the two "ContentPlaceHolder" controls from the "Master Page".

Within the 1st "ContentPlaceHolder" put a table that has a few rows with some link button controls like this:

Listing 3

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <table style="width:160px;vertical-align:top;">
        <tr>
            <td style="vertical-align:top">
                <asp:LinkButton ID="lb1" runat="server" OnClick="lbPanelShow_Click">lb1</asp:LinkButton>
            </td>
        </tr>
        <tr>
            <td>
             <asp:LinkButton ID="lb2" runat="server" OnClick="lbPanelShow_Click"> lb2 </asp:LinkButton>
            </td>
        </tr>
        <tr>
            <td>
            <asp:LinkButton ID="lb3" runat="server" OnClick="lbPanelShow_Click"> lb3</asp:LinkButton>
            </td>
        </tr>
        <tr>
            <td>
            <asp:LinkButton ID="lb4" runat="server" OnClick="lbPanelShow_Click"> lb4</asp:LinkButton>
            </td>
        </tr>
    </table>
</asp:Content>

We will work on the lbPanelShow_Click method when we get to the code behind.

Now we will add the section to the right where our content is going to go. We will add a panel for each of our link buttons:

Listing 4

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<table style="width:640px">
    <tr>
        <td>
        <asp:Panel ID="pnl1" runat="server" Visible="false">
         Panel 1
        </asp:Panel>
         <asp:Panel ID="pnl2" runat="server" Visible="false">
          Panel 2       
         </asp:Panel>
         <asp:Panel ID="pnl3" runat="server" Visible="false">
          Panel 3
         </asp:Panel>
         <asp:Panel ID="pnl4" runat="server" Visible="false">
          Panel 4       
         </asp:Panel>
        </td>
    </tr>
</table>
</asp:Content>

Ok, now that we have our UI Together, we can go to the code behind.

Understanding the Code

In the code behind, we need to add the lbPanelShow_Click which will handle the OnClick of all of the "LinkButtons" that we added, because we told it to earlier.

Listing 5

protected void lbPanelShow_Click(object sender, EventArgs e)
        {
            // your sender would be the link button that you clicked.            
            LinkButton oLbl = (LinkButton)sender;
            // now get the form
            Control oFrm = Page.Form;
            //loop through the Forms control collection
            foreach (Control oCtl in oFrm.Controls)
            {
            // you will find the place holder controls here       
            if (oCtl is ContentPlaceHolder)
                {
                  // loop through the placeholder's control collection
                  foreach(Control oCtl in ctl.Controls) {
                     // we only want to access our panel controls 
                     if (ctl1 is Panel)
                      {
                          switch (oLbl.Text) { 
                              case "lb1":
                                  if (ctl1.ID == "pnl1")
                                  {
                                      ctl1.Visible = true;
                                  }
                                  else
                                  {
                                      ctl1.Visible = false;
                                  }
                                  break;
                              case "lb2":
                                  if (ctl1.ID == "pnl2")
                                  {
                                      ctl1.Visible = true;
                                  }
                                  else
                                  {
                                      ctl1.Visible = false;
                                  }
                                  break;
                              case "lb3":
                                  if (ctl1.ID == "pnl3")
                                  {
                                      ctl1.Visible = true;
                                  }
                                  else
                                  {
                                      ctl1.Visible = false;
                                  }
                                  break;
                              case "lb4":
                                  if (ctl1.ID == "pnl4")
                                  {
                                      ctl1.Visible = true;
                                  }
                                  else
                                  {
                                      ctl1.Visible = false;
                                  }
                                  break;
                              default:
                                  break;
                          }                     
                      }
                  }
                }
            } //end foreach loop
        }// end procedure lbPanelShow_Click

Now that you have done that, try it out. You will see it performs rather nicely.

Summary

In this article, you have learned how to recurse through the controls in an ASP.NET 2.0 WebForm using Master Pages.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 7:38:51 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search