Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I cannot access master form objects in my code:
ASP.NET
//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">

C#
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
Updated 14-Feb-13 3:57am
v2
Comments
ZurdoDev 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

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900