ASP.NET C# DataList FindControl & ASP.NET C# DataList FindControl &页眉/页脚模板导致错误

发布于 2024-09-02 05:58:47 字数 2728 浏览 9 评论 0原文

每当我使用 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 技术交流群。

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

发布评论

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

评论(3

避讳 2024-09-09 05:58:47

DataList1_ItemDataBound 事件应类似于:

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();
    }
}

此方法将为 DataList 中的每个项目触发。当它到达页眉或页脚时,它找不到 bok_prisLabel 控件,因为它仅在 ItemTemplate 中声明,而不是在 HeaderTemplate 或 FooterTemplate 中声明。

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.

夏夜暖风 2024-09-09 05:58:47

当您添加页眉和页脚时,有时“项目”将是页眉和页脚,因此它找不到标签。您可以测试项目类型,但执行以下操作可能更容易。

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();
}

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();
}
偏爱你一生 2024-09-09 05:58:47

您需要检查 ListItemType

if (e.Item.ItemType == ListItemType.Header)
{
   //Find your control
}

You need to check for the ListItemType

if (e.Item.ItemType == ListItemType.Header)
{
   //Find your control
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文