Asp。Net,从继承的page类中获取对象?

时间:2022-09-24 23:41:47

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!

我有baslik。aspx页面,它使用一个母版。我创建了一个工人。cs档案相同的图书馆,我想从工人那里获得。baslik cs。aspx文字对象。它说空对象引用!

My baslik.aspx:

我的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 :

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... :

我想从Worker访问页面上(运行)的任何对象。所以我的尝试……:

public partial class Worker : Baslik
{
   public void ActionStart()
    { 
       this.myBaslikLiteral.Text = "Bla bla bla";
       //i can see my literal name but it is null.
    }
}

3 个解决方案

#1


2  

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.

在。net 1.1天里,事情变得更简单了;正如这个答案所提到的,控件通常是在定义良好的位置创建的,其方式类似于在InitializeComponent中实例化Windows Forms控件的方式。

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.

.Net 2.0时代改变了这一切;不再需要拥有所有这些锅炉板代码——相反,框架将根据page.designer.cs文件中声明的控件在第一次运行时(或者在站点被预先编译之前)生成所有必要的连接。

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).

不幸的是,当您从标准的ASPX页面继承时,只有在childs .designer.cs文件中声明的控件才会自动创建;父属性在父控件上是可用的,它们永远不会自动初始化(正如链接的答案所建议的,查看生成的代码非常有启发性)。

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).

相反,人们通常将自己的基本页面封装在一个独立的类中;这个类以编程方式构建必要的控件,例如,在CreateControlsMethod中,或者在基类中类似的类中,在页面生命周期的早期被调用(比如Page_Init)。但是,还要注意,有多少人仅仅将公共页面功能放在BasePage中(请参阅这里的一个典型示例),因为创建整个基本控件树非常繁琐,并且很难更改布局(几乎无法维护,因为您必须在后面有效地创建整个页面)。

However, a simpler way of encapsulating areas of functionality in the page (like common form fields) is just to create UserControls.

然而,在页面中封装功能区域的一种更简单的方法(像常见的表单字段)只是为了创建用户控件。

#2


0  

I think that myBaslikLiteral is private field inside generated by designer from Baslik.aspx.

我认为myBaslikLiteral是由Baslik.aspx的设计人员生成的私有字段。

One approach would be to expose this filed through protected or public property in Baslik.cs:

一种方法是通过baslikesc的受保护或公共财产公开该文件。

public Literal myPublicBaslikLiteral { get { return myBaslikLiteral; } }

Then in Worker.cs you can use this property like:

然后在工人。你可以使用这个属性:

this.myPublicBaslikLiteral.Text = "Bla bla bla";

#3


0  

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.

如果这不起作用,您可能需要通过页面上的控件枚举,然后找到子控件。

#1


2  

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.

在。net 1.1天里,事情变得更简单了;正如这个答案所提到的,控件通常是在定义良好的位置创建的,其方式类似于在InitializeComponent中实例化Windows Forms控件的方式。

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.

.Net 2.0时代改变了这一切;不再需要拥有所有这些锅炉板代码——相反,框架将根据page.designer.cs文件中声明的控件在第一次运行时(或者在站点被预先编译之前)生成所有必要的连接。

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).

不幸的是,当您从标准的ASPX页面继承时,只有在childs .designer.cs文件中声明的控件才会自动创建;父属性在父控件上是可用的,它们永远不会自动初始化(正如链接的答案所建议的,查看生成的代码非常有启发性)。

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).

相反,人们通常将自己的基本页面封装在一个独立的类中;这个类以编程方式构建必要的控件,例如,在CreateControlsMethod中,或者在基类中类似的类中,在页面生命周期的早期被调用(比如Page_Init)。但是,还要注意,有多少人仅仅将公共页面功能放在BasePage中(请参阅这里的一个典型示例),因为创建整个基本控件树非常繁琐,并且很难更改布局(几乎无法维护,因为您必须在后面有效地创建整个页面)。

However, a simpler way of encapsulating areas of functionality in the page (like common form fields) is just to create UserControls.

然而,在页面中封装功能区域的一种更简单的方法(像常见的表单字段)只是为了创建用户控件。

#2


0  

I think that myBaslikLiteral is private field inside generated by designer from Baslik.aspx.

我认为myBaslikLiteral是由Baslik.aspx的设计人员生成的私有字段。

One approach would be to expose this filed through protected or public property in Baslik.cs:

一种方法是通过baslikesc的受保护或公共财产公开该文件。

public Literal myPublicBaslikLiteral { get { return myBaslikLiteral; } }

Then in Worker.cs you can use this property like:

然后在工人。你可以使用这个属性:

this.myPublicBaslikLiteral.Text = "Bla bla bla";

#3


0  

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.

如果这不起作用,您可能需要通过页面上的控件枚举,然后找到子控件。