简体   繁体   中英

Design Advice using Master Pages

Master Page

  <div id="header" style="height: 150px; width: 750px;">
        <asp:Label ID="Label3" runat="server" Text="LoggedInUser:"></asp:Label>
        <asp:Label ID="lblLoggedInUser" runat="server" Text=""></asp:Label>
    </div>
    <div id="leftMenu" class="leftmenu">
          <br />
        <asp:DropDownList ID="ddlFamilyMembers" runat="server" 
              style="height: 25px; width: 125px" DataTextField="FullName" 
              DataValueField="MembershipGen" 
              onselectedindexchanged="ddlFamilyMembers_SelectedIndexChanged" 
              AutoPostBack="True" >
        </asp:DropDownList>
          <br /><br />
          <asp:Image ID="imageMember" class="space" runat="server" Height="150px" Width="125px" />
          <br /><br /><br />
        <asp:Label ID="Label1" runat="server" class="space" Text="MembershipID:"></asp:Label>
        <asp:Label ID="lblMembershipID" runat="server" Text=""></asp:Label>

        <br />
        <asp:Label ID="Label2" runat="server" class="space"  Text="Name:"></asp:Label>
        <asp:Label ID="lblMemberName" runat="server" Text=""></asp:Label>

        <br /><br /><br />
          <asp:LinkButton ID="lbInformation" class="space" runat="server" onclick="lbInformation_Click">Member Information</asp:LinkButton><br />
          <asp:LinkButton ID="lbAddress" class="space" runat="server" 
              onclick="lbAddress_Click">Member Address</asp:LinkButton>
    </div>

<div id="divRight" class="divright">
        <asp:ContentPlaceHolder ID="CPHMain" runat="server">
        </asp:ContentPlaceHolder>
        </div>

I posted my masterpage code so you can better understand my issue. Since these controls are in my master page I needed to create public properties for all the controls in my masterpage to databind my content pages. Maybe I am approaching this wrong but this is how I was planning on achieving this.

ContentPage Default.aspx page_load event would call my method to retrieve the data and set the dropdownlist, image, 2 label fields. On selected index change of dropdownlist it would grab the new values from another method and populate those controls accordingly.

To access these controls in my master page i read you can do it a couple of different ways one is to <%@MasterType VirtualPath="~/Member.Master" %> and then create a strongly typed connection. Or you could create a loosely type connection.

My problem with these ways as I am going to have over 15 content pages and I would hate to have to rebind the DropDownList every time one of the content pages is called. Also I would have to reinstantiate the controls in every method of my content pages which I assume I am doing this wrong.

Can someone give me some advice on what is the proper way to achieve this without so much repetitive code?

You've got the right idea, trying to decouple the parent from the child, but I think the way you want to do it may be clunky.

Assuming you can do this, what I'd do is as follows:

  • Make a public method BindFamilyMembers(string parameter) on the Master page's code-behind.

  • From the child page, pass necessary unique parameters to this BindFamilyMembers Master method.

This way you don't need to expose the controls themselves, you expose a method that modifies those controls based on parameters.

You want to notify the child page that the DDL SelectedIndex has changed. This is a great case for custom events.

You can configure an event on the Master page that the child page listens for. When DDL.SelectedIndex changes, you can fire the custom event (and pass the selected information) via the custom event, and any child page that is listening can handle that event.

There are a lot of examples of custom events online. Here's one you can start with: http://www.marten-online.com/csharp/simple-custom-event-handling.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM