如何将文本框输入传递给asp.net中的gridview ?

时间:2022-12-02 13:58:58

I have two textbox and one gridview in my webform. The gridview is binded with database. But I want to add one more column on runtime which will be the textbox input from the webform. Because the scenario is like: I am maintaining two formula to calculate some percentage using the two textbox and the client wants to see this calculation for each row in the gridview.

我的webform中有两个文本框和一个gridview。gridview与数据库绑定。但是我想在运行时上再添加一列,这是来自webform的文本框输入。因为场景是这样的:我使用两个文本框来维护两个公式来计算百分比,而客户端希望看到gridview中每一行的计算。

But I cannot do this.

但我做不到。

Is there anyone who can help me on this please? May be some suggestion.

有谁能在这件事上帮助我吗?可能是一些建议。

Thanks in advance.

提前谢谢。

1 个解决方案

#1


2  

You could add the column in your GridView markup with a label control to display the result as follows.

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

Here is the markup needed, please note Visible is set to false.

这是需要的标记,请注意可见被设置为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>

Use the RowDataBound event to find the label and calculate your result as below:

使用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;
   }
  }
 }
}

Please forgive any errors, I have not tested the above.

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

#1


2  

You could add the column in your GridView markup with a label control to display the result as follows.

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

Here is the markup needed, please note Visible is set to false.

这是需要的标记,请注意可见被设置为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>

Use the RowDataBound event to find the label and calculate your result as below:

使用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;
   }
  }
 }
}

Please forgive any errors, I have not tested the above.

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