Simple user control

B

Buddy Ackerman

Trying to create my first user control (one that really doesn't do much) and I keep getting an error saying "Object reference not set to an instance of an object." on the line "mylabel.Text = Property2;" in the Page_Load procedure. Seems too simple to have an error but I've managed to do it. What am I missing?


Here's my control:

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="WebUserControl1.ascx.cs"
Inherits="ServerControls.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>

<asp:Label ID="mylabel" Runat=server />



Here's my codebehind:



namespace ServerControls
{
using System;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;

public class WebUserControl1 : System.Web.UI.UserControl
{
public int Property1 = 0;
public string Property2 = "Default Content";
public Label mylabel;

private void Page_Load(object sender, System.EventArgs e)
{
mylabel.Text = Property2;
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}




Here's the web form I'm using it in:



<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ServerControls.WebForm1" %>
<%@ Register TagPrefix="aspdnsf" Namespace="ServerControls" Assembly="ServerControls" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<form id="Form1" method="post" runat="server">
<aspdnsf:WebUserControl1 Property1=1 Property2="Hello" runat="server"/>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top