删除Repeater中的列

时间:2016-04-19 19:49:09

标签: c# asp.net

我正在尝试删除标题单元格及其列,具体取决于字符串的内容。当我运行Web应用程序时,我收到错误:NullReferenceException由用户代码处理。

问题是我如何找到控件。它似乎是空的。但是,我的预期结果是如果条件为真,那么该列不应该是可见的(HTML透视图)。

我的代码背后代码:

protected void rptBillHeaders_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    RepeaterItem item = e.Item;
    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
    {
        Repeater rptLineItems = ((Repeater)item.FindControl("rptLineItems"));
        rptLineItems.DataSource = currentBill.LineItemsByVersion[rowNumber].Sort(BillLineItem.SortColumn.LineItemNumber, System.Data.SqlClient.SortOrder.Ascending);
        rptLineItems.DataBind();
        rowNumber++;
    }

    Control HeaderTemplate = rptBillHeaders.Controls[0].Controls[0];

    if (lit.ObjectType.C == "C")
    {
        e.Item.FindControl("DocColumn").Visible = false;
        e.Item.FindControl("DocHeader").Visible = false;
    }
}

在我的.aspx文件中,我有:

<asp:Repeater runat="server" ID="rptBillHeaders" OnItemDataBound="rptBillHeaders_ItemDataBound">
    <HeaderTemplate>
        <table width="98%" align="center" class="grid" cellpadding="0" cellspacing="0" style="border:solid 1px #000000; border-top:none 0px #FFFFFF;">
            <tr class="gridHeaderRow">
                <th>&nbsp</th>
                <th><asp:Label runat="server" ID="billVersionLabel" ></asp:Label></th> 
                <th>Action Type</th>
                <th id="DocHeader" runat="server">Doc #</th>
                <th>Status</th>
                <th>Total Amount</th>
                <th>Submitted By</th>
                <th>Date Submitted</th>
                <th>Date Processed</th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr class="gridRow gridRowThickTopBorder">
            <td style="padding: 5px;">
                <div id="divBillIcon" align="center" style="text-align:center;" class="icon ui-state-default ui-corner-all">
                    <span class="ui-icon ui-icon-plus"></span>
                </div>
            </td>
            <td class="Center"><%# ((int)Container.DataItem) %></td>
            <td><%# GetActionType((int)Container.DataItem) %> </td>
            <td id="DocColumn" runat="server"><%# GetDocNumber((int)Container.DataItem) %> </td>
            <td><%# val.get(cache.BillStatusDict, currentBill.ProcessedStatusesByVersion[((int)Container.DataItem)].Status, "&nbsp;") %></td>
            <td class="Right">$<%# currentBill.LineItemsByVersion[((int)Container.DataItem)].GetTotalAmount().ToString("2") %></td>
            <td><%# currentBill.PendingStatusesByVersion[((int)Container.DataItem)].CreateWebUserName %></td>
            <td><%# convert.ToDateString(convert.ToDateTimeQ(currentBill.PendingStatusesByVersion[((int)Container.DataItem)].CreateDate)) %></td>
            <td><%# convert.ToDateString(convert.ToDateTimeQ(currentBill.ProcessedStatusesByVersion[((int)Container.DataItem)].CreateDate)) %></td>
        </tr>

我尝试更改每个列中的标签类型,例如添加标签ID和单元格ID,但这会导致相同的错误。

2 个答案:

答案 0 :(得分:1)

您应该在自己的项目类型中查找每个控件:

protected void rptBillHeaders_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    ...

    if (item.ItemType == ListItemType.Header)
    {
        ...
        e.Item.FindControl("DocHeader").Visible = false;
    }
    else if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
    {
        ...
        e.Item.FindControl("DocColumn").Visible = false;
    }
}

答案 1 :(得分:0)

请从HeaderTemplate中删除您的Header并将其保留在Repeter之上。

<table width="98%" align="center">
    <tr class="gridHeaderRow">
        <th>&nbsp</th>
        <th>
            <asp:Label runat="server" ID="billVersionLabel"></asp:Label></th>
        <th>Action Type</th>
        <th id="DocHeader" runat="server">Doc #</th>
        <th>Status</th>
        <th>Total Amount</th>
        <th>Submitted By</th>
        <th>Date Submitted</th>
        <th>Date Processed</th>
    </tr>
    <asp:Repeater runat="server" ID="rptBillHeaders" OnItemDataBound="rptBillHeaders_ItemDataBound">
        <ItemTemplate>
            <tr class="gridRow gridRowThickTopBorder">
                <td style="padding: 5px;">
                    <div id="divBillIcon" align="center" style="text-align: center;" class="icon ui-state-default ui-corner-all">
                        <span class="ui-icon ui-icon-plus"></span>
                    </div>
                </td>
                <td class="Center"><%# ((int)Container.DataItem) %></td>
                <td><%# GetActionType((int)Container.DataItem) %> </td>
                <td id="DocColumn" runat="server"><%# GetDocNumber((int)Container.DataItem) %> </td>
                <td><%# val.get(cache.BillStatusDict, currentBill.ProcessedStatusesByVersion[((int)Container.DataItem)].Status, "&nbsp;") %></td>
                <td class="Right">$<%# currentBill.LineItemsByVersion[((int)Container.DataItem)].GetTotalAmount().ToString("2") %></td>
                <td><%# currentBill.PendingStatusesByVersion[((int)Container.DataItem)].CreateWebUserName %></td>
                <td><%# convert.ToDateString(convert.ToDateTimeQ(currentBill.PendingStatusesByVersion[((int)Container.DataItem)].CreateDate)) %></td>
                <td><%# convert.ToDateString(convert.ToDateTimeQ(currentBill.ProcessedStatusesByVersion[((int)Container.DataItem)].CreateDate)) %></td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

使用所需条件隐藏Repeater的OnItemDataBound事件上的所有条件标头和列。如下所示

protected void rptBillHeaders_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rptLineItems = ((Repeater)item.FindControl("rptLineItems"));
            rptLineItems.DataSource = currentBill.LineItemsByVersion[rowNumber].Sort(BillLineItem.SortColumn.LineItemNumber, System.Data.SqlClient.SortOrder.Ascending);
            rptLineItems.DataBind();
            var DocColumn = (HtmlTableCell)e.Item.FindControl("DocColumn");
            if (lit.ObjectType.C == "C")
            {
                if (DocColumn != null)
                {
                    DocColumn.Visible = false;
                    DocHeader.Visible = false;
                }
            }
            rowNumber++;
        }
    }