Quantcast 2008 March | Victor's Programming Aid - Part 5

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

Archive for March, 2008

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.

Though very simple once you see the example, it may not be obvious right off the bat. Note that a simple cast from a string to int will not convert a string of integers to an int!

string stringVariable = "1234";
int integerVariable = (int)stringVariable;

Therefore, to accomplish converting from a string to int, following the sample code below:

string stringVariable = "1234";
int integerVariable = Convert.ToInt32(stringVariable);

The Convert class is part of the System namespace, which is included in nearly every project by default. If for any reason your string of integers contains invalid characters, the function ToInt32 will throw a FormatException.

Another, though less popular way is to convert a string of integers to an int variable is to use the function Parse in the Int32 class. An example is below:

string stringVariable = "1234";
int integerVariable = Int32.Parse(stringVariable);

The reason most programmers use the Convert class over the Int32 class is because the Convert class has the power to convert to and from a wide selection of variable types. In the end of the day, it really is a matter of preference, but I suggest first time programmers to get in the habit of using the Convert class.

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.