用户控件中的数据列表提供了null引用

本文关键字:null 引用 列表 控件 数据 用户 | 更新日期: 2023-09-27 18:19:34

我将aspx页面上的Datalist控件移动到用户控件上,并将其引用到aspx页面。在这个过程中,我还移动了数据列表的源代码(ItemDataBound和Display方法)。我使用"数据表"作为数据列表填充的源。在我将控件移到用户控件后,当我尝试将数据表作为源分配给数据列表时,它会向我抛出NullReference权限。

public partial class Controls_ProductSpecifications : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //no code yet here...
    }
    public void DisplaySpecifications(SearchResultItem sri,bool IsMobilePage)
    {
        List<Category> breadcrumbCategories = sri.BreadcrumbCategories;
        Category templateCat =  breadcrumbCategories[breadcrumbCategories.Count - 1];
        ParametricColumnSortOrder customDimSortOrder = null;
        int rowPosition = 0; // manufacturer, product category, rohs
        bool isUnclassified = false;
        // -- get the sorted parametric list from database
        if (templateCat.Id.ToString() != null)
            customDimSortOrder = new ParametricColumnSortOrder(Convert.ToInt64(templateCat.Id), false, true);
        //List<ParametricAttributeGroup> plist = sri.ParametricDimensions;
        if (breadcrumbCategories[0].Name == "Unclassified")
        {
            ////if (breadcrumbCategories.Count > 1)
            ////{
            ////    //---{ Create an unclassifed attribute group } 
            ////    ParametricAttributeGroup pag = new ParametricAttributeGroup(breadcrumbCategories[0].Id, breadcrumbCategories[0].Name);
            ////    pag.ParametricAttributes.Add(new ParametricAttribute(breadcrumbCategories[1].Id, breadcrumbCategories[1].Name));
            ////    plist.Add(pag);
            ////}
        }
        var dt = new DataTable();
        dt.Columns.Add("Dimension");
        dt.Columns.Add("DimensionID");
        dt.Columns.Add("Attribute");
        dt.Columns.Add("AttributeID");
        dt.Columns.Add("CheckBox");
        dt.Columns.Add("CheckBoxState");
        DataRow dr;
        List<ParametricAttributeGroup> translatedAttGrp = sri.ParametricDimensions;
        LanguageDataAccess.GetTranslatedAttributes(translatedAttGrp, Language.CurrentLanguageCulture);
        foreach (ParametricAttributeGroup attributeGroup in translatedAttGrp)
        {
            ParametricAttribute attribute = attributeGroup.ParametricAttributes[0];
            dr = dt.NewRow();
            dr["Dimension"] = attributeGroup.Name;
            dr["DimensionID"] = attributeGroup.Id;
            dr["Attribute"] = attribute.Value;
            dr["AttributeID"] = attribute.Id;
            dr["CheckBox"] = "false";
            string grpname = attributeGroup.Name;
            // -- get the original name instead of the translated name for comparision --
            if (attributeGroup is LocalizationParametricAttributeGroup)
            {
                grpname = ((LocalizationParametricAttributeGroup)attributeGroup).OriginalName;
            }
            if (grpname == "Manufacturer")
            {
                dr["Dimension"] = Resources.lblManufacturer;
                dr["CheckBoxState"] = "0";
                dt.Rows.InsertAt(dr, rowPosition);
                rowPosition++;
            }
            else if (grpname == "Product Category")
            {
                // -- don't show product category if this product is under Unclassified --
                isUnclassified = breadcrumbCategories.Any(cat => cat.Name == Resources.MyMouser.lblUnclassified);
                if (!isUnclassified)
                {
                    dr["Dimension"] = attributeGroup.Name;
                    dr["Attribute"] = templateCat.Name;
                    dr["AttributeID"] = templateCat.Id;
                    dr["CheckBoxState"] = "0";
                    dt.Rows.InsertAt(dr, rowPosition);
                    rowPosition++;
                }
            }
            else if (grpname == "Unclassified")
            {
                dr["Dimension"] = Resources.lblUnclassified;
                dr["CheckBoxState"] = "0";
                dt.Rows.InsertAt(dr, rowPosition);
                rowPosition++;
            }
            else if (grpname == "RoHS - Mouser")
            {
                dr["Dimension"] = Resources.MyMouser.litHeaderRoHS;
                dr["Attribute"] = SearchHelper.CreateRoHSLabel(sri,sri.EnRoHSStatus, isUnclassified,IsMobilePage);
                dt.Rows.InsertAt(dr, rowPosition);
                rowPosition++;
            }
            else if (grpname == "Standard Pack Qty")
            {
                dr["Dimension"] = Resources.MyMouser.lblfactrPakcQty;
                dt.Rows.Add(dr);
            }
            else
            {
                dt.Rows.Add(dr);
            }
        }
        // -- sort the visiable fields --
        if (customDimSortOrder != null)
        {
            for (int dimSortIndex = 0; dimSortIndex < customDimSortOrder.Count; dimSortIndex++)
            {
                foreach (DataRow oldRow in dt.Rows)
                {
                    if (customDimSortOrder.AllKeys[dimSortIndex] == oldRow["DimensionID"].ToString())
                    {
                        dr = dt.NewRow();
                        dr["Dimension"] = oldRow["Dimension"];
                        dr["DimensionID"] = oldRow["DimensionID"];
                        dr["Attribute"] = oldRow["Attribute"];
                        dr["AttributeID"] = oldRow["AttributeID"];
                        dr["CheckBoxState"] = "0";
                        dt.Rows.Remove(oldRow);
                        dt.Rows.InsertAt(dr, rowPosition);
                        rowPosition++;
                        break;
                    }
                }
            }
        }
        //aliases information - shawn weng
        if (sri.Aliases != "")
        {
            dr = dt.NewRow();
            dr["Dimension"] = Resources.MyMouser.lblpartaliases;
            dr["DimensionID"] = 0;
            dr["Attribute"] = sri.Aliases;
            dr["AttributeID"] = 0;
            dr["CheckBox"] = "false";
            dt.Rows.Add(dr);
        }
        try
        {
            DataList dl1 = (DataList) this.FindControl("dlspec");
            dl1.DataSource = dt;
            dl1.DataBind();
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    protected void dlspec_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            && e.Item.DataItem != null)
        {
            // -- checkbox --
            var ck = e.Item.FindControl("FindSimilarCheckbox") as CheckBox;
            if (ck != null)
            {
                ck.ID = ck.Text;
                ck.Text = "";
                var lbldim = e.Item.FindControl("lblDimension") as Label;
                if (lbldim != null)
                {
                    if (lbldim.Text.Contains(Resources.MyMouser.lblManufacturer) || lbldim.Text.Contains(Resources.MyMouser.lblProductCategory))
                    {
                        ck.Checked = true;
                    }
                    if (lbldim.Text.Contains(Resources.MyMouser.lblfactrPakcQty))
                    {
                        string PackageQtylnk = "<a href=JAVASCRIPT:OpenFactoryQty();>" + Resources.MyMouser.lblfactrPakcQty + "</a>";
                        lbldim.Text = string.Format(PackageQtylnk);
                        lbldim.CssClass = "factorypackage";
                    }
                }
            }
        }
    }
}

这是ascx页面标记:

<%@ Control Language="C#" AutoEventWireup="true"       CodeBehind="ProductSpecifications.ascx.cs" Inherits="MouserWeb.Controls.Controls_ProductSpecifications" %>
 <asp:DataList ID="dlspec" runat="server" GridLines="Vertical" OnItemDataBound="dlspec_ItemDataBound">
<FooterStyle BackColor="#CCCCCC" />
<AlternatingItemStyle CssClass="alt-grey" />
<SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
    <table width="550px">
    <tr>
        <td class="leftcol">
        <asp:Label ID="lblDimension" runat="server" Text='<%# Eval("Dimension") %>'></asp:Label>:
        </td>
        <td class="ProductDetailData">
        <asp:Label ID="lblName" runat="server" Text='<%# Eval("Attribute") %>'></asp:Label>
        </td>
        <td class="find-similar">
        <asp:CheckBox ID="FindSimilarCheckbox" runat="server" Checked='<%# Eval("CheckBox")=="true"? true:false %>'
            Text='<%# Eval("AttributeID") %>' Visible='<%# Eval("CheckBoxState")=="0"? true:false %>' />
        <%--<input name='<%# Eval("DimensionID") %>' id='<%# Eval("DimensionID") %>' type="checkbox" checked='<%# Eval("CheckBox")=="true"? true:false %>' />--%>
        </td>
    </tr>
    </table>
</ItemTemplate>
</asp:DataList>

有什么不对劲吗??请让我知道,如果我能提供更多的信息(代码)关于这个。。。提前感谢您的宝贵帮助

用户控件中的数据列表提供了null引用

我不知道你在哪里调用DisplaySpecifications(...)来DataBind DataList

protected void Page_Load(object sender, EventArgs e)
{
    //no code yet here...
    if(!Page.IsPostBack)
    {
        DisplaySpecifications(...);
    }
}