用户控件中的数据列表在尝试分配数据源时抛出空引用异常

发布于 2024-12-25 02:11:11 字数 9354 浏览 0 评论 0原文

我将 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.MyMouser.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.MyMouser.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>

有什么问题吗?如果我可以提供有关此的更多信息(代码),请告诉我...提前感谢您的宝贵帮助

I moved a Datalist Control on an aspx page on to a usercontrol and referenced it to the aspx page. In the process, I also moved the source code for the datalist( ItemDataBound and Display methods). I am using a "Datatable" as the source for the Datalist population. after I move the control in to a user control, it throws me a NullReference right when i try to assign the datatable as a source to the datalist.

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.MyMouser.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.MyMouser.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";
                    }
                }
            }
        }
    }
}

here is the ascx page markup:

<%@ 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>

Does anything seem wrong?? Please let me know if i can provide more information(code) about this...Thanks in advance for your valuable help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千年*琉璃梦 2025-01-01 02:11:11

最可能的原因是未设置dt

下一个最相似的原因是异常实际上是在 dlspec_ItemDataBound 中引发的。

这就是为什么查看异常调用堆栈相当重要,如果有的话,将其包含在您的问题中。

要从代码中捕获的异常中获取大量有用信息,您可以调用它的 ToString 方法。例如,在您的 catch 块中:

Console.Write(ex.ToString();

更新

我对问题的根源有所了解,这是我们多次遇到的问题(假设您正在使用 Web 应用程序项目):

如果您移动通过仅从页面/用户控件源剪切并粘贴标记来将控件从一个页面/用户控件复制到另一个页面/用户控件而无需在进行更改后切换到设计模式,Visual Studio 并不总是更新中的引用设计师文件。

如果打开用户控件的设计器文件,则可以通过搜索 dlspec 来确认这一点。如果它不在那里,这就是您问题的根源。

因为我们已经多次遇到这种情况,所以每当我们像这样移动控件时,我们总是切换到设计模式,弄脏一些东西,然后保存。为了安全起见,我们通常会仔细检查设计器文件,以确保删除先前的条目并读取新的条目。

The most likely cause is that dt is not set.

The next most like cause is that the exception is actually thrown in dlspec_ItemDataBound.

This is why it is fairly critical to look at the exception call stack and, if available, include it in your question.

To get a good amount of useful information out of an exception that is caught in your code, you can call it's ToString method. For example, in your catch block:

Console.Write(ex.ToString();

Update

I have an idea about the source of the problem, which is one that we have encountered numerous times (assuming you are using a web application project):

If you move a control from one page/user control to another by just cutting and pasting the markup from the page/user control source without switching to design mode after making your changes, visual studio does not always update the references in the designer file.

If you open the designer file for the user control, you can confirm this by searching for dlspec. If it is not in there, this is the source of your problem.

Because we have been hit by this numerous times, whenever we move controls like this, we always switch into design mode, dirty something, then save. And to be safe we usually double-check the designer files to ensure the previous entry was removed and the new entry readded.

烛影斜 2025-01-01 02:11:11

如果您的 DataList 为空,则问题是由于 FindControl 找不到您的 datalist 控件造成的。这是 Scott Allen 关于此主题的一篇很好的文章。如果 DataTable 为 null,那么它会在代码中的某个位置被删除。

试试这个来查明问题:

DataTable dt1 = dt; // Set breakpoint here; check dt1 and dl1 to pinpoint prob
DataList dl1 = (DataList) this.FindControl("dlspec");
dl1.DataSource = dt1;
dl1.DataBind();

在我在评论中指出的位置设置断点,然后单步执行它以查看数据表是否为空。然后检查 FindControl 方法是否返回该控件或者它是否为 null。

If your DataList is coming out null, then the problem is a result of the FindControl not finding your datalist control. Here is a good article by Scott Allen on this topic. If the DataTable is null then its getting wiped out somewhere in your code.

Try this to pinpoint the problem:

DataTable dt1 = dt; // Set breakpoint here; check dt1 and dl1 to pinpoint prob
DataList dl1 = (DataList) this.FindControl("dlspec");
dl1.DataSource = dt1;
dl1.DataBind();

Set your breakpoint where I indicated in the comment and step thru it to see if the datatable is null. Then check and see if the FindControl method returns the control or if it's null.

风吹雨成花 2025-01-01 02:11:11

我想我解决了这个问题...
因此,从用户控件注册到的 aspx 页面的 Page_load() 调用 DisplaySpecification(...) 方法。
我将此调用移至 aspx 的 Page_PreRender() 事件,并且工作正常。
原因是用户控件实际上在 aspx page_load() 事件完成之前不会被加载。这就是为什么我一直将“dlspec”设置为空。
我感谢你们提供的所有帮助……你们太棒了。

再次感谢

I think I resolved this...
So the DisplaySpecification(...) method was being called from the Page_load() of the aspx page to which the user control is registered to.
I moved this call to the Page_PreRender() event of the aspx and it worked fine.
The reason being that the user control actually doesnt get loaded until the aspx page_load() event completes. That is why I had been getting the 'dlspec' as a null.
I appreciate all the help you guys have been...you're awesome.

Thanks Again

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文