ASP.NET中

时间:2016-03-26 01:26:21

标签: asp.net webforms

我使用附带的代码在ASP.NET(VB)中使用转发器控件在一个包含6列的表中填充一些数据。

我想要一段代码通过将if / elseif / else条件附加到转发器控件来实现以下功能:

  1. 对于包含<%#Eval(" SOLID")%>的记录=" 40103"然后将数据显示在一个单元格中,将所有6列合并为一个(理想情况下使用colspan = 6)。

  2. 对于具有<%#Eval(" SOLID")%>的记录=" 40105"然后将数据显示在两个单元格中,合并第二列和第三列。

  3. 对于所有其他记录,数据以实际格式显示。

  4. 我已经通过各种网站寻求解决方案,但徒劳无功。请帮帮我

    <asp:Repeater ID="Repeater3" runat="server">           
        <headertemplate>
            <tr style="height:25px;vertical-align:middle">
                <th class ="dg_rpt_center" align="left" width = "10%" >
                    <asp:Label ID="Label7"  runat="server" class ="lbl105" >SOLID</asp:Label></th>
                <th class ="dg_rpt_center" align="left" width = "30%">
                    <asp:Label ID="Label10"  runat="server" class ="lbl105" >SOL Name</asp:Label></th>
                <th class ="dg_rpt_center" align="right" width = "10%">
                    <asp:Label ID="Label11"  runat="server" class ="lbl105" >Target</asp:Label></th>
                <th class ="dg_rpt_center" align="right" width = "10%">
                    <asp:Label ID="Label12"  runat="server" class ="lbl105" >Achievement</asp:Label></th>      
                <th class ="dg_rpt_center" align="right" width = "10%">
                    <asp:Label ID="Label13"  runat="server" class ="lbl105" >Margin</asp:Label></th>
                <th class ="dg_rpt_center" align="right" width = "10%" >
                    <asp:Label ID="Label14"  runat="server" class ="lbl105" >Growth</asp:Label></th>
            </tr>
        </headertemplate>
        <ItemTemplate>
            <tr>
                <td align="left" width = "10%"><asp:LinkButton ID="lblsolid"  runat="server" class ="lbl9N" Text='<%#Eval("SOLID")%>' OnClick='subDtClick' CommandArgument='<%#Eval("SLNO")%>'></asp:LinkButton></td>
                <td align="left" width = "30%"><asp:Label ID="lblsolname"  runat="server" class ="lbl9N" Text='<%#Eval("NAME")%>'></asp:Label></td>
                <td align="right" width = "10%"><asp:Label ID="lbltgt"  runat="server" class ="lbl9N" Text='<%#Eval("TGT")%>'></asp:Label></td>
                <td align="right" width = "10%"><asp:Label ID="lblach"  runat="server" class ="lbl9N" Text='<%#Eval("ACH")%>'></asp:Label></td>
                <td align="right" width = "10%"><asp:Label ID="lblmrgn"  runat="server" class ="lbl9N" Text='<%#Eval("MGR")%>'></asp:Label></td>
                <td align="right" width = "10%"><asp:Label ID="lblgr"  runat="server" class ="lbl9N" Text='<%#Eval("GRW")%>'></asp:Label></td>
            </tr>
         </ItemTemplate>
    </asp:Repeater>
    

1 个答案:

答案 0 :(得分:0)

要实现此目的,您可以在Repeater控件的ItemDataBound事件中编写逻辑。您需要获取表格单元格和标签的引用,然后设置colspan,合并标签值,并隐藏任何额外的单元格。

ASPX页面

<asp:Repeater ID="Repeater3" runat="server" OnItemDataBound="Repeater3_ItemDataBound">
            <HeaderTemplate>
                <tr style="height: 25px; vertical-align: middle">
                    <th class="dg_rpt_center" align="left" width="10%">
                        <asp:Label ID="Label7" runat="server" class="lbl105">SOLID</asp:Label></th>
                    <th class="dg_rpt_center" align="left" width="30%">
                        <asp:Label ID="Label10" runat="server" class="lbl105">SOL Name</asp:Label></th>
                    <th class="dg_rpt_center" align="right" width="10%">
                        <asp:Label ID="Label11" runat="server" class="lbl105">Target</asp:Label></th>
                    <th class="dg_rpt_center" align="right" width="10%">
                        <asp:Label ID="Label12" runat="server" class="lbl105">Achievement</asp:Label></th>
                    <th class="dg_rpt_center" align="right" width="10%">
                        <asp:Label ID="Label13" runat="server" class="lbl105">Margin</asp:Label></th>
                    <th class="dg_rpt_center" align="right" width="10%">
                        <asp:Label ID="Label14" runat="server" class="lbl105">Growth</asp:Label></th>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td id="tdSOLID" runat="server" align="left" width="10%">
                        <asp:LinkButton ID="lblsolid" runat="server" class="lbl9N" Text='<%#Eval("SOLID")%>' CommandArgument='<%#Eval("SLNO")%>'></asp:LinkButton></td>
                    <td id="tdNAME" runat="server" align="left" width="30%">
                        <asp:Label ID="lblsolname" runat="server" class="lbl9N" Text='<%#Eval("NAME")%>'></asp:Label></td>
                    <td id="tdTGT" runat="server" align="right" width="10%">
                        <asp:Label ID="lbltgt" runat="server" class="lbl9N" Text='<%#Eval("TGT")%>'></asp:Label></td>
                    <td id="tdACH" runat="server" align="right" width="10%">
                        <asp:Label ID="lblach" runat="server" class="lbl9N" Text='<%#Eval("ACH")%>'></asp:Label></td>
                    <td id="tdMGR" runat="server" align="right" width="10%">
                        <asp:Label ID="lblmrgn" runat="server" class="lbl9N" Text='<%#Eval("MGR")%>'></asp:Label></td>
                    <td id="tdGRW" runat="server" align="right" width="10%">
                        <asp:Label ID="lblgr" runat="server" class="lbl9N" Text='<%#Eval("GRW")%>'></asp:Label></td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>

背后的代码

public void Repeater3_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {

            var lblsolid = (LinkButton)e.Item.FindControl("lblsolid");

            if (lblsolid.CommandArgument == "40103" || lblsolid.CommandArgument == "40105") {

                //Labels
                var lblsolname = (Label) e.Item.FindControl("lblsolname");
                var lbltgt = (Label) e.Item.FindControl("lbltgt");
                var lblach = (Label) e.Item.FindControl("lblach");
                var lblmrgn = (Label) e.Item.FindControl("lblmrgn");
                var lblgr = (Label) e.Item.FindControl("lblgr");

                //Table Cells
                var tdSOLID = (HtmlTableCell)e.Item.FindControl("tdSOLID");
                var tdNAME = (HtmlTableCell)e.Item.FindControl("tdNAME");
                var tdTGT = (HtmlTableCell)e.Item.FindControl("tdTGT");
                var tdACH = (HtmlTableCell)e.Item.FindControl("tdACH");
                var tdMGR = (HtmlTableCell)e.Item.FindControl("tdMGR");
                var tdGRW = (HtmlTableCell)e.Item.FindControl("tdGRW");

                if (lblsolid.CommandArgument == "40103") {
                    //merge all label values into a single string
                    var mergedCellValues = string.Format("{0} {1} {2} {3} {4}", lblsolname.Text, lbltgt.Text, lblach.Text, lblmrgn.Text, lblgr.Text);

                    //set the text value of the first table cell and span it 6 columns
                    tdSOLID.InnerText = mergedCellValues;
                    tdSOLID.Attributes.Add("colspan", "6");

                    //remove extra cells
                    tdNAME.Visible = false;
                    tdTGT.Visible = false;
                    tdACH.Visible = false;
                    tdMGR.Visible = false;
                    tdGRW.Visible = false;
                } else if (lblsolid.CommandArgument == "40105") {
                    //same concept as above, but with different colspan and cell hiding
                }

            }

        }
    }