Reputation: 4497
i have baslik.aspx page and it use a masterpage.I create a Worker.cs file same libruary and i want to access from Worker.cs to baslik.aspx Literal object.It says null object referance!
My baslik.aspx:
<%@ Page Async="true" EnableEventValidation="true" Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Baslik.aspx.cs" Inherits="F8.Imza.Baslik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
..
..
<asp:Literal runat="server" id="myBaslikLiteral">
..
</asp:Content>
baslik.aspx.cs :
namespace F8.Imza
{
public partial class Baslik : System.Web.UI.Page
{
...
...
...
}
}
And i want to access any object on page(running) from Worker.cs : so my try... :
public partial class Worker : Baslik
{
public void ActionStart()
{
this.myBaslikLiteral.Text = "Bla bla bla";
//i can see my literal name but it is null.
}
}
Upvotes: 3
Views: 2149
Reputation: 91550
Things were simpler back in the .Net 1.1 days; as this answer mentions, controls used to be created in a well defined place, in a fashion similar to how Windows Forms controls are instantiated in InitializeComponent.
The .Net 2.0 era changed all that; no longer was there any need to have all of this boiler plate code - instead, the framework would generate all of the necessary wiring up on first run (or before if the site was precompiled) based on the controls declared in the page.designer.cs file.
Unfortunately, when you then inherit from a standard ASPX page, only the controls declared in the childs child.designer.cs file get automatically created; the parent properties are available on the parent control, they are just never automatically initialized (as the linked answer recommends, taking a look at the generated code is very revealing).
Instead, it is very common for people to encapsulate their base page in a standalone class; this class builds the necessary controls programmatically, for example, in a CreateControlsMethod or similar in the base class, called early in the page lifecycle (like Page_Init). However, also note how many people simply put common page functionality in BasePage's (see here for a typical example) as creating your whole base control tree is very tedious and makes changing the layout difficult (almost unmaintainable as you have to effectively create the whole page in code behind).
However, a simpler way of encapsulating areas of functionality in the page (like common form fields) is just to create UserControls.
Upvotes: 2
Reputation: 224
Not entirely sure if I understand the question, but what if you try:
this.FindControl("myBaslikLiteral").Text = "Bla bla bla";
If that doesn't work, you may have to enumerate through the controls on the page then find the child control.
Upvotes: 0
Reputation: 5144
I think that myBaslikLiteral
is private field inside generated by designer from Baslik.aspx
.
One approach would be to expose this filed through protected or public property in Baslik.cs
:
public Literal myPublicBaslikLiteral { get { return myBaslikLiteral; } }
Then in Worker.cs
you can use this property like:
this.myPublicBaslikLiteral.Text = "Bla bla bla";
Upvotes: 0