从代码动态创建ASP.NET Web用户控件 - 未将对象引用设置为实例

时间:2011-10-10 15:38:51

标签: c# asp.net

基本上我正在尝试从代码创建我的Web用户控件的实例,并且我得到的对象引用未设置为实例异常。

以下是我的用户控件背后的代码。

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YodelShipping
{
    public partial class _Default : System.Web.UI.Page
    {
        private YodelLabel shippingLabel;

        protected void Page_Load(object sender, EventArgs e)
        {
            shippingLabel = (YodelLabel)LoadControl("~/YodelLabel.ascx");   
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            shippingLabel = new YodelLabel("express", "overnight", "Travel Toys", "555 Main Street", "Suite 102", "Hounslow", "middlesex", "TWJ 6JS", "John Doe", "0208 818 8000", "SALES", "Address Line 1", "Address Line 2", "TOWN", "County", "UB5 1AJ", "STD", "Day_Time", "1234586845", "YOUR #", "Your #", "1/1", "HAYES", "HATFIELD", "2LGBUB51aj+01000002", "(J)jd00 022 340 0100 0124");
            Controls.Add(shippingLabel);

        }
    }
}

这是我的用户控件的代码

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YodelShipping
{
    public partial class YodelLabel : System.Web.UI.UserControl
    {
        public YodelLabel()
        {

        }
        public YodelLabel(string productDescription, string serviceDescription, string fromCompanyName,
                          string fromAddressLine1, string fromAddressLine2, string fromTown, string fromCounty, string fromPostCode,
                          string toOrginizationName, string toPhoneNumber, string toDepartmentName, string toAddressLine1, string toAddressLine2,
                          string toTown, string toCounty, string toPostCode, string serviceCode, string dayTime, string shipmentNumber, string consignorReference, string cosigneeReference,
                          string pieceCount, string serviceCentre, string serviceHub, string routingCode, string licensePlate)
        {
            this.productDescription.Text = productDescription;
            this.serviceDescription.Text = serviceDescription;
            this.companyName.Text        = fromCompanyName;
            this.fromAddressLine1.Text   = fromAddressLine1;
            this.fromAddressLine2.Text   = fromAddressLine2;
            this.fromTown.Text           = fromTown;
            this.fromPostcode.Text       = fromPostCode;
            this.serviceCode.Text        = serviceCode;
            this.dayTime.Text            = dayTime;
            this.shipmentNo.Text         = shipmentNumber;
            this.consignorRef.Text       = consignorReference;
            this.consigneeRef.Text       = cosigneeReference;
            this.pieceCount.Text         = pieceCount;
            this.serviceCentre.Text      = serviceCentre;
            this.hub.Text                = serviceHub;
            this.routingCode.Text        = routingCode;
            this.licensePlate.Text       = licensePlate;

        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

我只是想创建一个新的控件实例,并使用构造函数填充控件上的标签。

1 个答案:

答案 0 :(得分:3)

我认为你在设置方面做错了。首先使用LoadControl实例化控件:

shippingLabel = (YodelLabel)LoadControl("~/YodelLabel.ascx");

然后使用构造函数创建另一个实例:

shippingLabel = new YodelLabel("express", "overnight", "Travel Toys", "555 Main Street", "Suite 102", "Hounslow", "middlesex", "TWJ 6JS", "John Doe", "0208 818 8000", "SALES", "Address Line 1", "Address Line 2", "TOWN", "County", "UB5 1AJ", "STD", "Day_Time", "1234586845", "YOUR #", "Your #", "1/1", "HAYES", "HATFIELD", "2LGBUB51aj+01000002", "(J)jd00 022 340 0100 0124");

您正在使用两种不同的方法创建两个不同的控件实例。你应该选择其中一个。