Steve Wellens

Programming in the .Net environment

Sponsors

Links

March 2009 - Posts

Master Page, Child Pages: Dynamic Communication Flexibility

Over on the ASP.NET forums, where I am a moderator, a user asked a question concerning Master Pages. I thought it was an interesting enough question to spend a little time on it and write a BLOG entry (golf season hasn't started in my state yet).

The question was how to have "static" buttons on a Master Page call functions in child pages when clicked. As usual, I wanted the simplest possible way to accomplish the goal.

The first thing to do is create a Master Page with three "Aux" buttons. I used a simple table to keep the buttons at the top of the form and they are all disabled by default. It will be up to the child pages to enable them as needed.

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
   <form id="form1" runat="server">
   <table>
      <tr><td>
         <asp:Button ID="_AuxButton1" runat="server" Text="Aux 1" Enabled="false" />
         <asp:Button ID="_AuxButton2" runat="server" Text="Aux 2" Enabled="false" />
         <asp:Button ID="_AuxButton3" runat="server" Text="Aux 3" Enabled="false" />
      </td></tr>
      <tr><td>
          <div style="background-color: #00FFFF">
              <br />
              This is Placeholder1:
              <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
              </asp:ContentPlaceHolder>
             </div>
       </td></tr>
   </table>
   </form>
</body>
</html>

Next, we expose the Master Page buttons as properties so the external entities, aka child pages, can have access to them:

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
 
    // expose the Aux buttons
    public Button AuxButton1
    {
        get { return _AuxButton1; }
    }
    public Button AuxButton2
    {
        get { return _AuxButton2; }
    }
    public Button AuxButton3
    {
        get { return _AuxButton3; }
    }
}

 

That's it for the Master Page. In the child page, we've set the MasterPageFile but there is one more thing we do to make life simpler: We set the MasterType property so the child pages "knows about" the type of Master Page:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="Child1.aspx.cs" Inherits="Child1" Title="Untitled Page" %>
 
<%@ MasterType TypeName="MasterPage" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    This is child content<br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</asp:Content>

Then, in the child page's Page_Load function, we hookup the buttons:

 
public partial class Child1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // hook up our events to the master pages buttons
        Master.AuxButton1.Click += Aux1_Click;
        Master.AuxButton2.Click += Aux2_Click;
 
        // by default, the buttons are disabled, if we are
        // going to use them, enable them
        Master.AuxButton1.Enabled = true;
        Master.AuxButton2.Enabled = true;
 
        // custom button text
        Master.AuxButton2.Text = "Click me!"
    }
 
    // functions called from Master Page
    protected void Aux1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Aux1Clicked!";
    }
    protected void Aux2_Click(object sender, EventArgs e)
    {
        Label2.Text = "Aux2 has been clicked!";
    }
}
 

Here is what the browser looks like after we've clicked the aux buttons:

  • Aux button 3 remains disabled because we did not connect it to any function.
  • Aux button 2 has custom text we applied.
  • The two Child Page label controls have been updated from the Master Page.

 

I hope someone finds this useful.

Steve Wellens

IE 8 Can Profile JavaScript

Being on the software "bleeding edge" is similar to being the "point man" in combat. I generally avoid being the first to adopt new technology until some other poor bastard has led the way…and occasionally paid a price: The price being missed deadlines, ulcers and ruined reputations. No thanks.

However, with Internet Explorer 8, for some inexplicable reason, I jumped right in. And I was pleasantly surprised. One of the nicest features is the new Developer Tools toolbar that comes with IE 8. Previously the Developer Toolbar was an add-on.

On the Developer Tools window there is a new Profiler tab. Since there have been recent performance issues on the Asp.Net forums where I'm a moderator, I thought I'd give it a spin.

  • I clicked Start Profiling.
  • I navigated to the Asp.Net forums website.
  • I waited for the page to finish loading. Having the Profiler running slows things down quite a bit…which is understandable.
  • After the page had finished loading, I clicked Stop Profiling.

It was that easy. The results are below. It appears the getAvatar function is taking a lot of time. If each call to getAvatar results in a database call, and each call results in an image being resized, that could explain why there are performance issues.

 

I hope you find this useful.

Steve Wellens.

More Posts