Expose HyperLink Control on Master Page using a Public Property

Last post 05-12-2008 1:53 PM by allanhorwitz. 3 replies.

Sort Posts:

  • Expose HyperLink Control on Master Page using a Public Property

    05-09-2008, 12:40 PM
    • Loading...
    • allanhorwitz
    • Joined on 04-10-2008, 9:45 AM
    • Philadelphia, PA
    • Posts 417

    I am trying to expose a hyperlink control on a master page to a content page. The following master page code behind throws an error during compilation.

    "Object reference not set to an instance of an object." for line 30.

    Please help me expose the property and compile without errors.

    1    using System;
    2    using System.Data;
    3    using System.Configuration;
    4    using System.Collections;
    5    using System.Web;
    6    using System.Web.Security;
    7    using System.Web.UI;
    8    using System.Web.UI.WebControls;
    9    using System.Web.UI.WebControls.WebParts;
    10   using System.Web.UI.HtmlControls;
    11   
    12   namespace MasterPageAndCSSDemo
    13   {
    14       public partial class Site1 : System.Web.UI.MasterPage
    15       {
    16   
    17           protected void Page_Load(object sender, EventArgs e)
    18           {
    19   
    20           }
    21   
    22           public string HyperLink1Text
    23           {
    24               get
    25               {
    26                   return this.HyperLink1.Text;
    27               }
    28               set
    29               {
    30                   this.HyperLink1.Text = value;
    31               }
    32           }
    33   
    34       }
    35   }
    

     

    <%@ Master Language="C#" AutoEventWireup="true" Codebehind="Site1.master.cs" Inherits="MasterPageAndCSSDemo.Site1" %>
    <!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>Master Page and CSS Demo</title>
        <link runat="server" id="csslnk1" href="~/Stylesheet1.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <table cellpadding="0" cellspacing="0" border="0" width="800px">
                    <tr>
                        <td colspan="3" class="header">
                            <br />
                            Master Page and CSS Demo
                            <br />
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td valign="top" width="120px" class="sidebar">
                            <asp:HyperLink ID="HyperLink1" NavigateUrl="~/WebForm1.aspx" runat="server">HyperLink1</asp:HyperLink>
                            <br />
                            <br />
                            <asp:HyperLink ID="HyperLink2" NavigateUrl="~/WebForm2.aspx" runat="server">HyperLink2</asp:HyperLink>
                        </td>
                        <td>
                            <img id="img1" runat="server" alt="" src="~/Image/spacer.gif" style="width: 30px;
                                height: 10px" />
                        </td>
                        <td valign="top">
                            <asp:ContentPlaceHolder ID="MainBody" runat="server">
                            </asp:ContentPlaceHolder>
                                                                                       
                                              
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3" class="footer">
                            Demo Applications</td>
                    </tr>
                </table>
            </div>
        </form>
    </body>
    </html>
    
     
    Allan Horwitz
  • Re: Expose HyperLink Control on Master Page using a Public Property

    05-09-2008, 1:57 PM
    • Loading...
    • haoest
    • Joined on 10-25-2005, 8:20 PM
    • Posts 403

    depending on where on your calling page you want to get the hyperlink's property, it may or may not have been initialized. Refer to page life cycle doc:

    http://msdn.microsoft.com/en-us/library/ms178472.aspx

     

    Debugger is my best friend. (http://haoest.info)
  • Re: Expose HyperLink Control on Master Page using a Public Property

    05-09-2008, 2:09 PM
    • Loading...
    • allanhorwitz
    • Joined on 04-10-2008, 9:45 AM
    • Philadelphia, PA
    • Posts 417

    It turns out the error is thrown due to code in the ContentPage Page_Load event:

     

    1    using System;
    2    using System.Data;
    3    using System.Configuration;
    4    using System.Collections;
    5    using System.Web;
    6    using System.Web.Security;
    7    using System.Web.UI;
    8    using System.Web.UI.WebControls;
    9    using System.Web.UI.WebControls.WebParts;
    10   using System.Web.UI.HtmlControls;
    11   
    12   namespace MasterPageAndCSSDemo
    13   {
    14       public partial class WebForm1 : System.Web.UI.Page
    15       {
    16           protected void Page_Load(object sender, EventArgs e)
    17           {
    18               HyperLink ctrl = (HyperLink)Master.FindControl("HyperLink1");
    19               ctrl.Enabled = false;
    20   
    21               Site1 obj = new Site1();
    22               obj.HyperLink1Text = "TEST";
    23   
    24           }
    25       }
    26   }
    
     

     

    When the parser reaches line 22, the parser goes to the master page code behind before the actual HyperLink1 control is created.

    So, I guess I am correctly exposing the HyperLink1 control. But, it is only functional after the master page code behind is processed.

     

    Allan Horwitz
  • Re: Expose HyperLink Control on Master Page using a Public Property

    05-12-2008, 1:53 PM
    Answer
    • Loading...
    • allanhorwitz
    • Joined on 04-10-2008, 9:45 AM
    • Philadelphia, PA
    • Posts 417

    Here is the working code from the content page:

     

    <%@ Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MasterPageAndCSSDemo.WebForm1" %>
    <%@ MasterType VirtualPath="~/Site1.Master" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainBody" runat="server">
    <div class="subheader">Web Form 1</div>
    <br />
    
    Ut faucibus. Cras id est nec nibh malesuada lobortis. Morbi nulla sem, euismod eu, vehicula nec, pretium nec, lacus. Vivamus vel metus.</asp:Content>
    
     

     

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    namespace MasterPageAndCSSDemo
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                HyperLink ctrl = (HyperLink)Master.FindControl("HyperLink1");
                if (ctrl != null)
                    ctrl.Enabled = false;
    
                //Because Properties have been created on the master page code behind, the value of 
                //the conrols enabled property can be changed with the following code as well.
                Master.HyperLink1Enabled = false;
            }
    
        }
    }
    
     
    Allan Horwitz
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter