Tips Station Asp.net Article Seo Articles
Tutorials Code Samples
›  Home
›  Mission
›  About us
›  Contact Us
›  Feedback
›  Terms & Condition
Asp Articles
IT Solutions
 
› ASP.NET

› Programming Tips

› Ajax

› Asp

› ADO.NET

› Databases

› SEO

› CSS And Designing

› Php

 
Most Viewed Articles
 
› Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

› Change theme dynamically without page refresh in ASP.NET

› Install AJAX On Machines Running Visual Studio 2005

› Creating Pretty Popups Using AJAX

› Simple ASP Image Resize Function

› SQL Server Performance Counters

› ASP.Net Interview Questions And Their Answers

› Encode Url using ASP

› Difference Between DataGrid and GridView in Asp.Net

› Select Specific Value WithIn Drop Down List Or Radio Button List

more...
 
 
Home> ASP.NET
 
Access Master Page controls from the Content Page - Asp.net
 
An example of how you can get or set the values of a control on the Master Page from one of your content pages.
One of the questions that has been cropping up on the forums a lot recently, is how to interact with with controls on the master page from one of your Content Pages.

There are a couple of ways I can think of to do this, but the method I prefer is to use Properties. To demonstrate how we create and use these Properties, imagine we have a Master Page which contains a Label control (we'll just call it "Label1"), and we want to show it's value in a TextBox on our Content Page. Our Master Page, would look like this:
<%@ Master Language="VB" CodeFile="site.master.vb" Inherits="site" %>  
<!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>Untitled Page</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:Label ID="Label1" runat="server" Text="Testing"></asp:Label>  
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">  
        </asp:contentplaceholder>  
    </div>  
    </form>  
</body>  
</html>  
 
and our Content Page would look like this:
<%@ Page Language="VB" MasterPageFile="~/site.master" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1"  %>  
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
    <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>  
</asp:Content>  
 
Now, we need some way of getting the Master Page to expose the Text property of it's "Label1" control. This is where the Properties come in and to set this up we need to make the Property Public so that it can be accessed by other pages. So, our code behind for the Master Page will now consist of:
Partial Class site  
    Inherits System.Web.UI.MasterPage  
     Public Property MyText()  
        Get  
            Return Label1.Text  
        End Get  
        Set(ByVal value)  
            Label1.Text = value  
        End Set  
    End Property  
End Class  
You'll notice that there is both a Get and a Set method for this Property which will allow us to both retrieve and update the value. To do this from our Content Page, we first need to get a reference to the Master Page, which can be done easily by using the Page.Master property, and then we need to cast the Master Page to the type of Master we are using so that we can see the Public Property and Get the value. We can also use the same methods to Set the property, as is shown by looking at the code behind for the Content Page:
Partial Class Default1  
    Inherits System.Web.UI.Page  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
        ' Set the value of the Master Page's Label to the Content Page's TextBox  
        txtTest.Text = CType(Page.Master, site).MyText  
        ' Update the Master Page's Label  
        CType(Page.Master, site).MyText = "Changed"  
    End Sub  
End Class  
   
When you run this code in your development environment, you'll see that the first Label shows our "Changed" text, whereas the TextBox will show the previous value which we retrieved via the Get method.

This article shows you a simple example of how to interact with controls on the Master Page, and will give you the necessary logic to build on this code and apply it to similar situations where you need the interaction between the two pages.
 
 
 
Vrp Technologies
 
Serversea Hosting
 
 
Latest Articles
 
› Sending SMS With PHP

› MySQL Join Tutorial

› Make An RSS Feed Using PHP

› Intro To Object: Option Variables

› Design An Online Chat Room With PHP And MySQL

› Create Tell A Friend Script With HTML & PHP

› Benchmark And Optimize PHP Script Speed

› What Kind of DBA Are You?

› SQL Server Performance Counters

› SQL Server Performance Tips

more...
 
Random Articles
 
› Add a Confirmation popup to a Button in Asp.Net

› Find which control caused a postback - Asp.net

› Displaying Data using ASP.NET 2.0 Repeater

› Clear all Cached objects in Asp.Net

› Understanding DDL Triggers In SQL Server 2005

› Choosing a Web Site Hosting Provider for an ASP.NET Site

› The Most Powerful SEO Tactic: Simplify, Simplify, Simplify

› Successful CSS Template Skins

› Creating Pretty Popups Using AJAX

› Design Considerations for Cross Page Post Backs in ASP.NET 2.0

more...
 
Home Mission About us Contact us Feedback Terms Conditions
2008 © Copyright TipsStation. All rights reserved.