我的转发器中有嵌入式gridview。在转发器itemdatabound上我绑定gridview并尝试检索将在gridviewfooters中显示的总数,但是,总计不起作用,我缺少什么? 请有人帮忙 干杯
<asp:Repeater ID="rpt" runat="server" onitemdatabound="rpt_ItemDataBound" >
<HeaderTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblName" runat="server" ClientIDMode="Static"></asp:Label>
</td>
<td>
<asp:Label ID="lblAddress" runat="server" ClientIDMode="Static"></asp:Label>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hfID" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.link_id") %>' />
<asp:GridView ID="grd" ClientIDMode="Static" runat="server"
Width="100%" DataKeyNames="ID" AutoGenerateColumns="False" GridLines="Vertical"
CellPadding="4" AllowPaging="True" AllowCustomPaging="True" PageSize="25" PagerStyle-Visible="False"
ShowFooter="true" OnRowDataBound="grd_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfID" runat="server" ClientIDMode="Static" Value='<%# DataBinder.Eval(Container,"DataItem.id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="code" DataField="code" />
<asp:BoundField HeaderText="Description" DataField="desc" />
<asp:BoundField HeaderText="Qty" DataField="quantity" />
<asp:BoundField HeaderText="Employee Paid" DataField="e_total" />
<asp:BoundField HeaderText="Client Charged" DataField="c_total" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
背后的代码
double dEmpTotal = 0;
double dClientTotal = 0;
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
int ID = int.Parse(((HiddenField)e.Item.FindControl("hfID")).Value);
GridView grd = (GridView)e.Item.FindControl("grd");
DataContext dc = new DataContext(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
var qry = from p in dc.usp_list_costs(ID)
select p;
List<usp_list_costsResult> lstCosts = new List<usp_list_costsResult>();
lstCosts = qry.ToList();
double dEmpTotal = Convert.ToDouble(lstCosts.Sum(r => r.emp_total));
double dClientTotal = Convert.ToDouble(lstCosts.Sum(r => r.client_total));
grd.DataSource = lstCosts;
grd.DataBind();
}
}
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "Employee Total:" + dEmpTotal;
e.Row.Cells[5].Text = "Client Total:" + dClientTotal;
}
}
答案 0 :(得分:0)
我假设您的总数始终为零,因为您在处理行类型为Footer的grd_RowDataBound时尝试获取dEmpTotal
和dClientTotal
值,但此时值已设置为零。 / p>
您必须在rpt_ItemDataBound
事件处理程序中将数据绑定到转发器项时设置总值。
因此您根本不需要在grd_RowDataBound
方法中设置页脚行的值。
代码应该是这样的(只是将最后两行添加为我的示例代码)
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
int ID = int.Parse(((HiddenField)e.Item.FindControl("hfID")).Value);
GridView grd = (GridView)e.Item.FindControl("grd");
DataContext dc = new DataContext(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
var qry = from p in dc.usp_list_costs(ID)
select p;
List<usp_list_costsResult> lstCosts = new List<usp_list_costsResult>();
lstCosts = qry.ToList();
double dEmpTotal = Convert.ToDouble(lstCosts.Sum(r => r.emp_total));
double dClientTotal = Convert.ToDouble(lstCosts.Sum(r => r.client_total));
grd.DataSource = lstCosts;
grd.DataBind();
grd.FooterRow.Cells[4].Text = "Employee Total:" + dEmpTotal;
grd.FooterRow.Cells[5].Text = "Client Total:" + dClientTotal;
}
}