Sunday 15 April 2012

c# - Data List Header Template causing 'Object reference not set to an instance of an object' error -


itemprop = "text">

I have a DataList that I repeat the item and turn on the automatic (line for an application I'm working on) Want to use color. I'm trying to add header template above the item template in this way.

  & lt; Table & gt; & Lt; ASP: DataList ID = "DataList1" runat = "server" CellPadding = "4" DataKeyField = "number" DataSourceID = "SqlDataSource1" ForeColor = "# 333333" OnItemDataBound = "DataList1_ItemDataBound" & gt; & Lt; AlternatingItemStyle BackColor = "White" /> & Lt; FooterStyle BackColor = "# 507CD1" font-bold = "true" ForeColor = "white" /> & Lt; Header Style BackAcllor = "# 507CD1" font-bold = "true" ForeColor = "white" /> & Lt; Item System BackColor = "#EFF3FB" /> & Lt; HeaderTemplate & gt; & Lt; TR & gt; & Lt; Th & gt; Item name & lt; / Th & gt; & Lt; Th & gt; Item type & lt; / Th & gt; & Lt; Th & gt; Item price & lt; / Th & gt; & Lt; Th & gt; In the end (s) & lt; / Th & gt; & Lt; / TR & gt; & Lt; / HeaderTemplate & gt; & Lt; ItemTemplate & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; A class = "rolls2" href = "ItemView.aspx ITEMNAME = & lt;?% # Eval (" ITEMNAME ")%> & amp; num = & lt;% # Eval (" number ")%> "& Gt; & Lt;% # Eval ("item name")%> & Lt; / A & gt; & Lt; / Td> & Lt; Td> & Lt;% # Eval ("item type")%> & Lt; / Td> & Lt; Td> $ & Lt;% # Eval ("ItemPrice")%> & Lt; / Td> & Lt; Td> & Lt; Asp: label id = "lblExp" text = '& lt;% # bind ("create")% & gt; Runat = "server" /> gt; & Lt; / Td> & Lt; / TR & gt; & Lt; / ItemTemplate & gt; & Lt; SelectedItemStyle BackColor = "# D1DDF1" font-bold = "true" ForeColor = "# 333333" /> & Lt; / ASP: DataList & gt; & Lt; Table & gt;   

When I run this code, no object is set in the context of the object and it represents lblExp . If I remove the header, then the project runs successfully. Is there another way to add only one header in my table?

It's hard to diagnose without seeing the code, but I think it will be for you < Code> DataList1_ItemDataBound to search the lblExp in Control e.Item Trying e without trying .emem.ItemType first.

This event will be picked up for each item in the list, including the header, and there is no control in the header named lblExp .

You need to check the itemtype before the item process:

  Protected Zero DataList1_ItemDataBound (Object Sender, DataListItemEventArgs e) {switch (e. Item.ItemType) {Case ListItemType. Item: Case ListItemType.AlternatingItem: Case ListItemType.SelectedItem: Case ListItemType.EditItem: {labeled lblExp = (label) e.Item.FindControl ("lblExp"); ...}}}    

No comments:

Post a Comment