Simple user control

Discussion in 'ASP .Net Web Controls' started by Buddy Ackerman, Nov 3, 2005.

  1. 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>
     
    Buddy Ackerman, Nov 3, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Rowton
    Replies:
    2
    Views:
    672
    Tom Rowton
    Aug 1, 2003
  2. vatech1993
    Replies:
    4
    Views:
    5,693
    =?Utf-8?B?U8Opcmdpbw==?=
    Dec 11, 2004
  3. Replies:
    0
    Views:
    437
  4. Mad Scientist Jr
    Replies:
    0
    Views:
    647
    Mad Scientist Jr
    Mar 22, 2006
  5. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    558
    Jonathan Wood
    Feb 2, 2010
Loading...

Share This Page