ASP.NET C# DataList FindControl & ASP.NET C# DataList FindControl &页眉/页脚模板导致错误
每当我使用 DataList 的页眉或页脚模板时,FindControl 都无法找到 DataList 的标签部分,并抛出 NullReferenceException。
我的 SQLDataSource 和 DataList(无页眉和页脚模板 - 有效)
<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>
:页脚模板 - 不起作用。
<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>
选择事件:
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();
}
为什么会发生这种情况?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DataList1_ItemDataBound 事件应类似于:
此方法将为 DataList 中的每个项目触发。当它到达页眉或页脚时,它找不到 bok_prisLabel 控件,因为它仅在 ItemTemplate 中声明,而不是在 HeaderTemplate 或 FooterTemplate 中声明。
The DataList1_ItemDataBound event should look something like:
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.
当您添加页眉和页脚时,有时“项目”将是页眉和页脚,因此它找不到标签。您可以测试项目类型,但执行以下操作可能更容易。
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.
您需要检查 ListItemType
You need to check for the ListItemType