2011-04-20 67 views
1

我有一个页脚gridview,并显示在页脚价格列的总数。我想要访问页脚中的值并将其显示在gridview外部的文本框中。需要在gridview外的文本框gridview中的页脚总数

这是我的GridView的外观仅页脚模板

<asp:TemplateField HeaderText="Total" > 
<ItemTemplate> 
<asp:Label ID="lbltotal" runat="server" Text='' ></asp:Label> 
</ItemTemplate> 
<FooterTemplate> 
<asp:Label ID="lbltotalprice" runat="server" Text=''></asp:Label> 
</FooterTemplate> 
</asp:TemplateField> 

下面就是我如何在页脚中显示总

In gridview rowdatabound event 
    if (e.Row.RowType == DataControlRowType.Footer) 
      { 
       Label amounttotal = (Label)e.Row.FindControl("lbltotalprice"); 
       amounttotal.Text = String.Format("{0:C2}", total); 
      } 

我试图在以下方式中的另一种方法

GridViewRow row = GridView1.FooterRow; 
Total.Text= ((Label)row.FindControl("lbltotalprice")).ToString();--- does not help at all 

请在texbox gridview的外部访问在页脚这个值帮助。 在此先感谢。

回答

1

使用文本框代替标签试试吧。文本框可以用相同的语法访问,但是我看到了标签问题。

string a = ((TextBox)row.FindControl("TextBox1")).Text; 
1

你可以尝试设置这个ItemDataBound事件之外,一旦你的列表已经被绑定和项目金额已全部填充(这样就可以检索这些值,并计算出总的)。如何做到这一点的一个例子如下:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     MyGrid.DataSource = GetDataSource(); 
     MyGrid.DataBind(); 

     SetTotalInGridFooter(); 
    } 
} 

private void SetTotalInGridFooter() 
{ 
    double total = 0; 

    foreach (RepeaterItem ri in in MyGrid.Items) 
    { 
     if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem) 
     { 
      double d; 

      Label lbltotal = (Label) ri.FindControl("lbltotal"); 

      if (Double.TryParse(lbltotal.Text, out d)) 
       total += d; 

      continue; 
     } 

     if (ri.ItemType == ListItemType.Footer) 
     { 
      Label lbltotalprice = (Label) ri.FindControl("lbltotalprice"); 
      lbltotalprice.Text = String.Format("{0:C2}", total); 

      break; 
     } 
    } 
}