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

C# 从未设置为实例的代码对象引用动态创建ASP.NET Web用户控件,c#,asp.net,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 cl

基本上,我试图从代码中创建一个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)
        {

        }
    }
}

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

我认为您的安装有问题。首先,使用
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");

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

我认为你的安装有问题。首先,使用
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");

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

在哪一行出现异常?请给出一条建议,看看您传递给YodelLabel构造函数的参数数量。考虑传递一个具有这些属性的对象。当我点击页面上的按钮来添加一个新的用户控件时,它会抛出异常。在哪条线上你得到异常?只是一条建议,看看你传递给YoDelabeLL构造函数的参数个数。考虑传递具有这些属性的一个对象。当单击页面上的按钮以添加新的用户控件时,它会引发异常。