如何在asp.net中动态添加页脚模板

时间:2013-05-22 04:49:21

标签: asp.net

我动态创建了一个gridview。在设计部分中描述了模板字段。所有列都是通过代码创建的,如下所示。它的工作正常。在这里,我可以列出每行的页面。但我不知道如何通过代码隐藏在页脚模板中实现页面总和。

TemplateField Pages = new TemplateField();
Pages.HeaderText = "Pages";
Pages.ItemTemplate = new GridViewTemplate_Pages();
gv1.Columns.Add(Pages);


    public class GridViewTemplate_Pages : ITemplate
    {

        void ITemplate.InstantiateIn(Control container)
        {
            Label PagesLabel = new Label();
            PagesLabel.DataBinding += new EventHandler(this.PagesLabel_DataBinding);
            container.Controls.Add(PagesLabel);
        }

        void PagesLabel_DataBinding(object sender, EventArgs e)
        {
            Label lbl1 = (Label)sender;
            GridViewRow row = (GridViewRow)lbl1.NamingContainer;
            lbl1.Text = DataBinder.Eval(row.DataItem, "PagesReceived").ToString();
        }
    }

在aspx页面中给出ShowFooter =“True”并单独写入RowDataBound。如果我在aspx页面中给出了页脚模板但是不知道如何以编程方式获得结果,则以下代码可以正常工作。请指教。

    protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int RowTotalPages = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PagesReceived"));
            TotalPages = TotalPages + RowTotalPages;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label m = (Label)e.Row.FindControl("gv1TotalPages");
            m.Text = TotalPages.ToString();
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以像这样为网格视图创建页脚。

//代码

GridView gv = new GridView();
gv.RowCreated += delegate(object dsender, GridViewRowEventArgs ge)
{
    if (ge.Row.RowType == DataControlRowType.Footer)
        ge.Row.Cells[0].Text = "Something";
};
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
BoundField bf = new BoundField();
bf.HeaderText = "col 1";
bf.DataField = "Length";
gv.Columns.Add(bf);
gv.DataSource = new string[] { "One", "Two", "Three" };
gv.DataBind();
Form.Controls.Add(gv);

这适用于动态创建的gridview和页脚。你可以相应地改变。

答案 1 :(得分:0)

这是一件愚蠢的事。我让它变得复杂。我需要页脚行中页面的总和,那么我为什么要思考和混淆GridViewTemplate和Itemplate类。我只是在会话中添加了Total页面,并在DataControlRowType.Footer中重新收集它。这是代码:

    protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int RowTotalPages = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PagesReceived"));
            int RowTotalCharges = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ChargesReceived"));
            TotalPages = TotalPages + RowTotalPages;
            TotalCharges = TotalCharges + RowTotalCharges;
            Session.Add("TotalPages", TotalPages.ToString());
            Session.Add("TotalCharges", TotalCharges.ToString());
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[4].Text = "Batches Count: " + gv1.Rows.Count.ToString();
            e.Row.Cells[5].Text = Session["TotalPages"].ToString();
            e.Row.Cells[6].Text = Session["TotalCharges"].ToString();
        }
    }