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

Posts Tagged ‘ page ’

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.

Round trip database reads becomes an issue with large databases. You may notice this occurs in grid views. Each time the dataset is sorted or filtered, the default grid view needs to be data binded. In most cases, the data source is the database (sql statement or stored procedure). An easy way to prevent these round trips across page loads is to use a session variable. One important aspect to note is that session variables expire by default after fifteen minutes.

We are going to store a Boolean variable rememberMe in a ViewState. By storing the Boolean in a ViewState, rememberMe will retain its value across page loads. In this example, the boolean variable rememberMe will default to false. The variable rememberMe can be read to and from as if any public class variable.

public partial class _Default : System.Web.UI.Page
{
  public bool rememberMe
  {
    get
    {
      object o = ViewState["rememberMe "];
      return (o == null)? false : (bool)o;
    }
    set
    {
      ViewState["rememberMe"] = value;
    }
  }

  protected void Page_Load(object sender, EventArgs e)
  {

  }

}

Again, it is important to note that session variables expire by default after fifteen minutes. If a default value is not assigned, additional logic is required to prevent unanticipated affects. This method is very effective when retrieving an extremely large number of records. You will immediately notice the first query will take the standard query time, but each subsequent action on the same dataset will be very quick.