views:

1304

answers:

3

Hello,

whenever I use the Header or Footer template of DataList, FindControl is unable to find a label part of the DataList, and throws a NullReferenceException.

My SQLDataSource and DataList (no Header and Footer template - works):

        <asp:SqlDataSource ID="sdsMinaKop" runat="server" 
        ConnectionString="<%$ ConnectionStrings:dbCSMinaKop %>"      
        SelectCommand="SELECT kopare_id, bok_id, bok_titel, bok_pris, kop_id FROM kop WHERE kopare_id = @UserName" 
        onselecting="sdsMinaKop_Selecting">
        <SelectParameters>
            <asp:Parameter DefaultValue="admin" Name="UserName" />
        </SelectParameters>
    <asp:SelectParameters>
        <asp:Parameter Name="UserName" Type="String" />
    </asp:SelectParameters>
    </asp:SqlDataSource>

    <asp:DataList ID="DataList1" runat="server" DataKeyField="kop_id" 
        DataSourceID="sdsMinaKop" onitemdatabound="DataList1_ItemDataBound" 
            RepeatLayout="Table">
        <ItemTemplate>
        <tr>
        <td><asp:Label ID="bok_titelLabel" runat="server" Text='<%# Eval("bok_titel") %>' /></td>
        <td><asp:Label ID="bok_prisLabel" runat="server" Text='<%# Eval("bok_pris") %>' /> 
            kr</td>
        <td><a href="avbestall.aspx?id='<%# Eval("kop_id") %>'" />[X]</a></td>
        </tr>
        </ItemTemplate>
        <ItemStyle Wrap="False" />
    </asp:DataList>

With Header & Footer template - does not work.

<asp:DataList ID="DataList1" runat="server" DataKeyField="kop_id" 
        DataSourceID="sdsMinaKop" onitemdatabound="DataList1_ItemDataBound" 
            RepeatLayout="Table">
        <ItemTemplate>
        <tr>
        <td><asp:Label ID="bok_titelLabel" runat="server" Text='<%# Eval("bok_titel") %>' /></td>
        <td><asp:Label ID="bok_prisLabel" runat="server" Text='<%# Eval("bok_pris") %>' /> 
            kr</td>
        <td><a href="avbestall.aspx?id='<%# Eval("kop_id") %>'" />[X]</a></td>
        </tr>
        </ItemTemplate>
        <ItemStyle Wrap="False" />
        <HeaderTemplate>
            a
        </HeaderTemplate>
        <FooterTemplate>
            a
        </FooterTemplate>
    </asp:DataList>

Selecting event:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    Label pris = (Label)e.Item.FindControl("bok_prisLabel");

    LabelTotalt.Text = (Convert.ToDouble(LabelTotalt.Text) + Convert.ToDouble(pris.Text)).ToString();
}

Why would this happen?

Thanks

+1  A: 

You need to check for the ListItemType

if (e.Item.ItemType == ListItemType.Header)
{
   //Find your control
}
TheGeekYouNeed
+1  A: 

when you add the header and footer, sometimes the "item" will be the header and footer so it does not find the label. you can test for itemtype but it might be easierto do thefollowing.

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    Label pris = e.Item.FindControl("bok_prisLabel") as Label; //won't fail if null returned

    if (pris !=null)
        LabelTotalt.Text = (Convert.ToDouble(LabelTotalt.Text) + Convert.ToDouble(pris.Text)).ToString();
}
Ben Robinson
m3n
+1  A: 

The DataList1_ItemDataBound event should look something like:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Label pris = (Label)e.Item.FindControl("bok_prisLabel");

        LabelTotalt.Text = (Convert.ToDouble(LabelTotalt.Text) + Convert.ToDouble(pris.Text)).ToString();
    }
}

This method will fire for each item in your DataList. When it gets to the header or footer, it can't find the bok_prisLabel control because it is only declared in the ItemTemplate, not the HeaderTemplate or FooterTemplate.

John Allers