用户控件中的数据列表给出了空引用

时间:2012-01-06 15:50:16

标签: c# asp.net user-controls datalist

我将aspx页面上的Datalist Control移动到usercontrol并将其引用到aspx页面。在这个过程中,我还移动了datalist的源代码(ItemDataBound和Display方法)。我使用“数据表”作为Datalist人口的来源。将控件移动到用户控件后,当我尝试将数据表作为源分配给数据列表时,它会向我抛出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>

有什么事吗?如果我能提供更多关于此的信息(代码),请告知我......提前感谢您的宝贵帮助

1 个答案:

答案 0 :(得分:0)

我看不到您向DataBind DisplaySpecifications(...)调用DataList的位置?

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