我已经动态地创建了一个gridview。设计部分描述了模板字段。所有的列都是通过后面的代码创建的,如下所示。它的工作很好。在这里我可以列出每一行的页面。但我不知道如何通过代码后面的页脚模板实现页面总和。如何在asp.net中动态添加页脚模板
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();
}
}
给定ShowFooter =“True”在aspx页面和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();
}
}
它是一个愚蠢的事情。我做得很复杂。我需要页脚行中的页面总和,那么为什么我应该考虑一下GridViewTemplate和Itemplate类。只需在会话中添加Total页面并在DataControlRowType.Footer中将其重新收录。下面是代码: – Aruna
保护无效gv1_RowDataBound(对象发件人,GridViewRowEventArgs E) { 如果(e.Row.RowType == DataControlRowType.DataRow) { INT RowTotalPages = Convert.ToInt32(的DataBinder.Eval(e.Row。 DataItem,“PagesReceived”)); TotalPages = TotalPages + RowTotalPages; Session.Add(“TotalPages”,TotalPages.ToString()); (e.RowType == DataControlRowType.Footer) {e.Row.Cells [5] .Text = Session [“TotalPages”]。ToString(); } } – Aruna