Valerie Meunier

Valerie Meunier

  • 1.5k
  • 693
  • 64.4k

The object reference is not set to an instance of an object

Dec 31 2022 3:07 PM

Hi

I'm trying to reach Label1 in aspx file from another class than the partial class , but i get the error: at the bold line below:

System.NullReferenceException: The object reference is not set to an instance of an object

Yet, i created an instance for Label1. Thansk for help. V.

using System;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public string a="ok";
        protected void Page_Load(object sender, EventArgs e)
        {
            Test ts = new Test();
            ts.Txtch();
        }
    }
    class Test : WebForm1
    {
        public void Txtch()
        {
            WebForm1 wb = new WebForm1();
            wb.Label1.Text = a;
        }
    }
}

aspx file
---------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" inherits="WebApplication1.WebForm1" %>
...
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
...

Designer.cs
-----------
public global::System.Web.UI.WebControls.Label Label1;


Answers (3)