Click here to Skip to main content
12,950,084 members (61,595 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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
Updated 14-Feb-13 3:57am
v2
Comments
ryanb31 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
Top Experts
Last 24hrsThis month
OriginalGriff 5,829
CHill60 3,460
Maciej Los 2,953
Jochen Arndt 1,975
ppolymorphe 1,820


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2017
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