ASP.NET C# DataList FindControl & Header/Footer template causes error
- by m3n
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