问题描述
大家好,
我动态创建了一个gridview。在设计部分中描述了模板字段。所有列都是通过代码创建的,如下所示。它的工作正常。在这里,我可以列出每行的页面。但我不知道如何通过代码隐藏在页脚模板中实现页面总和。
Hi All,
I have created a gridview dynamically. There is template fields described in designing portion. All the columns were created thru code behind as follows. Its works fine. Here I can listed the pages for each rows. But I dont know how to implement the sum of pages in the footer template thru code behind.
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页面中给出了页脚模板但是不知道如何以编程方式获得结果,则以下代码可以正常工作。请给我建议。
Given ShowFooter="True" in aspx page and RowDataBound written separately. The following code works fine if I given footer template in aspx page but do not know how to get the result in programmatically. Please advice.
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();
}
}
推荐答案
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;
Session.Add("TotalPages", TotalPages.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();
}
}
这篇关于如何在asp.net中动态添加页脚模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!