3

我的网络表单中有两个文本框和一个网格视图。网格视图与数据库绑定。但我想在运行时再添加一列,这将是来自网络表单的文本框输入。因为场景是这样的:我正在维护两个公式来使用两个文本框计算一些百分比,并且客户端希望在 gridview 中查看每一行的这个计算。

但我不能这样做。

请问有人可以帮我吗?可能是一些建议。

提前致谢。

4

1 回答 1

2

您可以在 GridView 标记中添加带有标签控件的列,以显示结果,如下所示。

这是所需的标记,请注意 Visible 设置为 false。

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField Visible="false">
    <ItemTemplate>
        <asp:Label ID="label1" runat="server"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

使用 RowDataBound 事件查找标签并计算结果如下:

void GridView1GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{ 
 if(e.Row.RowType == DataControlRowType.DataRow)
 {
  //find the control
  var label1 = e.Item.FindControl("label1") as Label;
  if (label1 != null)
  {
   if (!string.IsNullOrEmpty(tbInput1.Text) && !string.IsNullOrEmpty(tbInput2.Text))
   {
      // Do the calculation and set the label
      label1.Text = tbInput1.Text + tbInput2.Text;
      // Make the column visible
      GridView1.Columns[0].Visible = true;
   }
  }
 }
}

请原谅任何错误,我没有测试过以上。

于 2013-06-10T09:28:27.557 回答