RowDataBound如何打印多个标签

时间:2014-07-04 08:47:12

标签: asp.net rowdatabound

我正在尝试计算两个日期之间的日期差异,并希望在网格中显示它们。我在网格中创建了一个表格,结果可以在那里打印。

我的RowDataBound正在计算所有内容,但不知道如何打印结果。

protected void grdADR_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblADRYear = (Label)e.Row.FindControl("lblADRYear");
            Label lblBRCIDADR = (Label)e.Row.FindControl("lblBRCIDADR");
            DataTable dt = new DataTable();

        string strQuery = "Select ADR_TYPE,DatePART(dayofyear,Document_Date) AS                       DayOfTheYearADR,Document_Date From ADR Where BRCID = '" + lblBRCIDADR.Text.ToString() + "'"
                        + " AND CAST(YEAR(Document_Date) AS VARCHAR(4)) = '" + lblADRYear.Text.ToString() + "' Order by DayOfTheYearADR";

        dt = objAccess.GetDataTable(strQuery);
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                int DayMarker = 0;
                int LastDay = 0;

                List<Label> labels = new List<Label>();


                for (int rcnt = 0; rcnt < dt.Rows.Count; rcnt++)
                {

                    DataRow dr = dt.Rows[rcnt];
                    Label lblADRTimeLineStart = new Label();
                    lblADRTimeLineStart.ForeColor = System.Drawing.Color.Red;
                    int RowDay = int.Parse(dr["DayOfTheYearADR"].ToString());
                    if (LastDay != RowDay)
                    {
                        Label lblEmptyBlock = new Label();
                        lblADRTimeLineStart.ForeColor = System.Drawing.Color.Blue;
                        lblEmptyBlock.Width = (RowDay - DayMarker) * 3;
                        labels.Add(lblEmptyBlock);


                        Label lblADRBlock = new Label();
                        lblADRTimeLineStart.ForeColor = System.Drawing.Color.Green;
                        lblADRBlock.Width = 3;
                        labels.Add(lblADRBlock);
                        DayMarker = RowDay + 3;

                    }
                    LastDay = RowDay;
                }

            }
        }

    }

}

ASPX文件

<asp:GridView ID="grdADR" runat="server" DataSource='<%# functADRTimeLine(Eval("Month").ToString()) %>'  AutoGenerateColumns="false" ShowHeader="true" AllowPaging="false" Width="1260px" GridLines="Both"
    BorderWidth="0" BorderColor="#839168" BorderStyle="Solid" CellPadding="0" BackColor="white" OnRowDataBound="grdADR_RowDataBound">



    <Columns>
        <asp:TemplateField Visible="false">
            <HeaderTemplate>BRCID</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblBRCIDADR" runat="server" Text='<%# Bind("BRCID") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Width="0px" Wrap="true" BorderColor="black" BorderStyle="Dotted" BorderWidth="1" HorizontalAlign="Center" />
            <HeaderStyle BorderColor="black" BorderStyle="Outset" BorderWidth="1" HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField Visible="false">
            <HeaderTemplate>Year</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblADRYEAR" runat="server" Text='<%# Bind("ADR_YEAR") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Width="0px" Wrap="true" BorderColor="black" BorderStyle="Dotted" BorderWidth="1" HorizontalAlign="Center" />
            <HeaderStyle BorderColor="black" BorderStyle="Outset" BorderWidth="1" HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField>

            <ItemTemplate>
                <table cellpadding="0" cellspacing="0" border="0">

                    <tr>
                        <td style="width: 150px">
                            <%--HERE I WANT TO PRINT MY RESULTS.--%> 
                        </td>

                        <td style="width: 1101px"></td>
                    </tr>
                </table>

            </ItemTemplate>

        </asp:TemplateField>



    </Columns>
    <HeaderStyle Height="25px" HorizontalAlign="Center" BackColor="#a8b194" />
    <RowStyle CssClass="grd_RowStyle" BorderColor="black" BorderStyle="Dotted" BorderWidth="1" Wrap="true" />
</asp:GridView>

1 个答案:

答案 0 :(得分:0)

您可以使用多种方法打印文本 一种选择是

在您希望打印标签的位置添加占位符控件

<asp:PlaceHolder ID="placeHolderLabels" runat="server"></asp:PlaceHolder>

在代码隐藏中,找到这样的占位符

PlaceHolder placeHolderLabels= (PlaceHolder)e.Row.FindControl("placeHolderLabels");

然后使用以下代码将标签添加到此占位符控件

placeHolderLabels.Controls.Add(lblEmptyBlock); 
placeHolderLabels.Controls.Add(lblADRBlock);

另一种选择是,由于您正在打印类似于结构的表格,因此您可以在要打印标签的位置添加datalist / gridview / repeater控件。然后,您可以将计算列表绑定到此控件,该控件更清晰