An array of C#, PHP, and HTML programming articles, tutorials, and resources

Posts Tagged ‘ content ’

When you use a content page based on a previously created master page, there is sometimes a want to access attributes of control defined in the master page, such as a label, hyperlink, or dropdownlist. By default, there is no way to easy way to access the those controls. However, using the FindControl function will allow access to those controls. First part of the example is the basic master page code:

<html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
      <title>Accessing Master Page Controls</title>
      <asp:ContentPlaceHolder ID="head" runat="server">
      </asp:ContentPlaceHolder>
   </head>
   <body>
      <form id="form1" runat="server">
         <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
            <asp:Label ID="lblUserName" runat="server" Text="Victor Chen"></asp:Label>
         </div>
      </form>
   </body>
</html>

Next, is a relevant snippet from the content page’s code behind file that will access a label defined earlier in the master page.

public partial class WebForm2 : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      String username = ((Label)Master.FindControl("lblUserName")).Text;
   }
}

The Master property listed in red will access the master page as defined in the <%@ Page MasterPageFile> property defined in the content page.

The FindControl function of the Master property takes one string input. The string input is the id of the control defined in the master page. In this case, the label id in the master page is defined as lblUserName.

Finally, the FindControl function returns an object, so we must cast the object back to a Label. Once back to a label, the text of the label can be extracted by accessing the Text property.

An updated Article Available at: Easily Convert An Existing Web Form to a Web Content Form (with a Master Page)

To convert an exist ing aspx page without a masterpage site template is easy. We are going to walk through a simple example for which can be applied to other situations. Assume you have the following page called Default.aspx (with it’s associated Default.aspx.cs page which we will not need to worry about).

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!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>Hello World</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <div>Hello World</div>
    </form>
  </body>
</html>

Now a standard masterpage with two content place holders (one for header information and the other for the main body content) is created for your web application called Site1.Master. Header and footer text has been added that will appear for every content item.

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.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" >
<html>
  <head runat="server">
    <title>Untitled Page</title>
    <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder>
  </head>
  <body>
    Hello World Header!
    <form id="form1" runat="server">
      <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
      </div>
    </form>
    Hello World Footer!
  </body>
</html>

First, an <%@ Page> tag is should be added to the first line. See resulting code for necessary tag attributes. Any page specific tags from the within the head tags except for <title> are placed inside the <asp:Content ID=”Content1″>. If there are no specific tags, this section will be present, but empty. Likewise, all content in the body (labeled in green in the above example), will be placed into <asp:Content ID=”Content2″>. To give your item template a title, there is a field in <%@ Page> called title which contains the page title (labeled in red in the above example). The final Default.aspx page will be as follows:

<%@ Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.WebForm1" Title="Hello World" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">Hello World
</asp:Content>

A regular webpage has now been converted to work with your defined masterpage! You can follow this as a basis to larger more complexed pages.