如何在Gridview页脚添加2行

4

我正在使用网格来显示潜在客户的数量。在此之中,我需要显示每页总数和总计。是否可以在页脚中以两个不同的行显示?请给我一些建议。我需要在网格中添加8列。

2个回答

4
你可以用多种方式来实现这一点,但其中一种方法是使用TemplateField。
以下是你的GridView格式(将你的内容放入单元格中)... ...
    <Columns>
        <asp:TemplateField>
            <FooterTemplate>
                <table width="100%">
                    <tr><td><asp:Literal runat="server" ID="ltField1" Text='<%# Bind("field1") %>'></asp:Literal></td>
                    </tr>
                    <tr><td>><asp:Literal runat="server" ID="ltField2" Text='<%# Bind("field2") %>'></asp:Literal></td>
                    </tr>
                </table>
            </FooterTemplate>

...


朋友,我需要将它添加到页脚中。这样我就可以为列元素和页脚元素分配单独的链接。 - susanthosh
只需将headertemplate切换为footertemplate。 - mson
你可以在 <td></td> 中放置任何内容。 - mson

2

您需要通过继承GridView类型来创建自定义的GridView类。

namespace CustomControls
{
 public class CustomGridView : GridView
    {
        private string _pageTotal;

        public string PageTotal
        {
            get { return _pageTotal; }
            set { _pageTotal = value; }
        }

        private string _grandTotal;

        public string GrandTotal
        {
            get { return _grandTotal; }
            set { _grandTotal = value; }
        }

        public CustomGridView()
        {
        }

        protected override void OnRowCreated(GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.SetRenderMethodDelegate(CreateFooter);
            }
            base.OnRowCreated(e);
        }

        private void CreateFooter(HtmlTextWriter PageOutput, Control FooterContainer)
        {
            StringBuilder footer = new StringBuilder();
            footer.Append("<td>" + this._pageTotal  +"</td>");
            footer.Append("</tr>");
            footer.Append("<tr>");
            footer.Append("<td>" + this._grandTotal + "</td>");
            footer.Append("</tr>");
            PageOutput.Write(footer.ToString());          
        }
    }
}

然后使用“Register”页面指示符引用您的自定义控件。
<%@ Register TagPrefix="cc" Namespace="CustomControls" %>

将您的控件添加到页面中,确保ShowFooter设置为true。
<cc:CustomGridView ID="GridView1" ShowFooter="true"></cc:CustomGridView>

您可以设置'PageTotal'和'GrandTotal'属性。
GridView1.PageTotal = "5";
GridView1.GrandTotal = "10";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接