Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Masterpage
I cannot access master form objects in my code:
//Mater form Script
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication2.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></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 179px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1">
            <tr>
                <td class="style2">
                    <asp:TreeView ID="TreeView1" runat="server" 
                        onselectednodechanged="TreeView1_SelectedNodeChanged">
                        <nodes>
                            <asp:TreeNode Text="New" Value="New">
                            <asp:TreeNode Text="Edi" Value="Edi">
                            <asp:TreeNode Text="Delete" Value="Delete">
                        </nodes>
                    
                </td>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                    
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
 
//Content form Script
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>
<%@ MasterType VirtualPath="~/Site1.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Master.treevew1  //  Here I cannot access masterform objects
        }
    }
Please help
Posted 13-Feb-13 22:11pm
Edited 14-Feb-13 3:57am
v2
Comments
ryanb31 at 14-Feb-13 10:00am
   
It's hard to tell exactly what your problem is, but I think you are saying that you can't access MasterPage objects from the Page that is using the master? If so, try adding <%@ MasterType VirtualPath="~/Master.Master" %> to your aspx page.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Master.treevew1  //  Here I cannot access masterform objects
You cannot directly access controls of master page.
 
You need to either strongly couple the master page with child page in page tag OR you need to use FindControl to find the control in master page OR you can expose a public property in Master page that exposes the needed server control.
 
Pick the one you like.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 465
1 Sergey Alexandrovich Kryukov 310
2 George Jonsson 269
3 Gihan Liyanage 255
4 OriginalGriff 245
0 OriginalGriff 4,903
1 CPallini 3,960
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,786
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100