从gridview访问文本框值

时间:2013-10-29 18:52:52

标签: c# asp.net

我有一个动态网格视图......它有四列..我想要的是从最后一列“支付金额”访问gridview中的文本框值,并在标签中显示其总和值。下面是我试过的代码。有人能让我知道怎么做吗?

ASP.NET

<asp:gridview ID="Gridview2" runat="server" ShowFooter="true" CssClass="vutblrow" 
                                                                TabIndex="3" HeaderStyle-CssClass="vutblhdr"
                                                                CellPadding="4" ForeColor="#333333" GridLines="None"  Width="1%"  
                                                                PagerStyle-Mode="NumericPages" 


            AutoGenerateColumns="false" onrowcreated="Gridview2_RowCreated" Height="16px">
              <PagerStyle CssClass="pgr"  Height="25px" BorderStyle="Solid" />
            <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Serial Number" />
            <asp:TemplateField HeaderText="From Place">
                <ItemTemplate>

                    <asp:TextBox ID="Textfrom" runat="server" CssClass="txtBoxNormalmedium"></asp:TextBox>
                </ItemTemplate>

            </asp:TemplateField>

             <asp:TemplateField HeaderText="To Place">
                <ItemTemplate>
                    <asp:TextBox ID="Textto" runat="server" CssClass="txtBoxNormalmedium"></asp:TextBox>
                </ItemTemplate>

            </asp:TemplateField>

             <asp:TemplateField HeaderText="Distance Travelled (kms)">
                <ItemTemplate>
                    <asp:TextBox ID="TextBoxdist" runat="server" CssClass="txtBoxNormalmedium"></asp:TextBox>
                </ItemTemplate>

            </asp:TemplateField>




           <asp:TemplateField HeaderText="Amount Paid (per km)">
                <ItemTemplate>
                    <asp:TextBox ID="TextBoxamt" runat="server" CssClass="txtBoxNormalmedium"></asp:TextBox>
                </ItemTemplate>

                <FooterStyle HorizontalAlign="Right" />
                <FooterTemplate>
                 <asp:Button ID="ButtonAdd1" runat="server" Text="Add New Row"
                         CssClass="btnNormalAdd" OnClick="add" />
                </FooterTemplate>
            </asp:TemplateField>
                 <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton2" runat="server" 
                        CssClass="lnkbut" OnClick="LinkButton2_Click">Remove</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:gridview>


                                                         <asp:Label ID="lblTotal" runat="server" Text="Label"></asp:Label>

C#代码:

namespace Test.Test
{
   public partial class WebForm6 : System.Web.UI.Page
   {
      protected void Page_Load(object sender, EventArgs e)
      {
         if (!IsPostBack)
         {
            SetInitialRow1();
         }
      }

      private void AddNewRowToGrid1()
      {
         int rowIndex = 0;

         if (ViewState["CurrentTable"] != null)
         {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
               for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
               {
                  //extract the TextBox values
                  TextBox box1 = (TextBox)Gridview2.Rows[rowIndex].Cells[1].FindControl("Textfrom");
                  TextBox box2 = (TextBox)Gridview2.Rows[rowIndex].Cells[2].FindControl("Textto");
                  TextBox box3 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("TextBoxdist");
                  TextBox box4 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("TextBoxamt");

                  drCurrentRow = dtCurrentTable.NewRow();
                  drCurrentRow["RowNumber"] = i + 1;

                  dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                  dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                  dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
                  dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;

                  rowIndex++;
               }
                  dtCurrentTable.Rows.Add(drCurrentRow);
                  ViewState["CurrentTable"] = dtCurrentTable;
                  Gridview2.DataSource = dtCurrentTable;
                  Gridview2.DataBind();
           }
        }
        else
        {
           Response.Write("ViewState is null");
        }
        //Set Previous Data on Postbacks
        SetPreviousData1();
   }

   private void SetPreviousData1()
   {
      int rowIndex = 0;
      if (ViewState["CurrentTable"] != null)
      {
         DataTable dt = (DataTable)ViewState["CurrentTable"];
         if (dt.Rows.Count > 0)
         {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
               TextBox box1 = (TextBox)Gridview2.Rows[rowIndex].Cells[1].FindControl("Textfrom");
               TextBox box2 = (TextBox)Gridview2.Rows[rowIndex].Cells[2].FindControl("Textto");
               TextBox box3 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("TextBoxdist");
               TextBox box4 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("TextBoxamt");

               box1.Text = dt.Rows[i]["Column1"].ToString();
               box2.Text = dt.Rows[i]["Column2"].ToString();
               box3.Text = dt.Rows[i]["Column3"].ToString();
               box4.Text = dt.Rows[i]["Column4"].ToString();

               rowIndex++;
             }
          }
      }
   }

   protected void add(object sender, EventArgs e)
   {
      AddNewRowToGrid1();
   }

   protected void Gridview2_RowCreated(object sender, GridViewRowEventArgs e)
   {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
         DataTable dt = (DataTable)ViewState["CurrentTable"];
         LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
         if (lb != null)
         {
            if (dt.Rows.Count > 1)
            {
               if (e.Row.RowIndex == dt.Rows.Count - 1)
               {
                  lb.Visible = false;
               }
            }
            else
            {
               lb.Visible = false;
            }
         }
      }
   }

   protected void LinkButton2_Click(object sender, EventArgs e)
   {
      LinkButton lb = (LinkButton)sender;
      GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
      int rowID = gvRow.RowIndex + 1;
      if (ViewState["CurrentTable"] != null)
      {
         DataTable dt = (DataTable)ViewState["CurrentTable"];
         if (dt.Rows.Count > 1)
         {
            if (gvRow.RowIndex < dt.Rows.Count - 1)
            {
               //Remove the Selected Row data
               dt.Rows.Remove(dt.Rows[rowID]);
            }
         }
         //Store the current data in ViewState for future reference
         ViewState["CurrentTable"] = dt;
         //Re bind the GridView for the updated data
         Gridview2.DataSource = dt;
         Gridview2.DataBind();
      }
      //Set Previous Data on Postbacks
     SetPreviousData1();
   }  

   private void SetInitialRow1()
   {
      DataTable dt = new DataTable();
      DataRow dr = null;
      dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
      dt.Columns.Add(new DataColumn("Column1", typeof(string)));
      dt.Columns.Add(new DataColumn("Column2", typeof(string)));
      dt.Columns.Add(new DataColumn("Column3", typeof(string)));
      dt.Columns.Add(new DataColumn("Column4", typeof(string)));

      dr = dt.NewRow();
      dr["RowNumber"] = 1;
      dr["Column1"] = string.Empty;
      dr["Column2"] = string.Empty;
      dr["Column3"] = string.Empty;
      dr["Column4"] = string.Empty;

      dt.Rows.Add(dr);
      //dr = dt.NewRow();

      //Store the DataTable in ViewState
      ViewState["CurrentTable"] = dt;

      Gridview2.DataSource = dt;
      Gridview2.DataBind();
   }

    }
}

2 个答案:

答案 0 :(得分:5)

 GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
 TextBox TextBox1 = row.FindControl("TextBox1") as TextBox; 

  //Access TextBox1 here.
  string myString = TextBox1.Text;

文本框是gridview行中的子控件,因此您可以为每个网格视图行迭代上面的代码。

答案 1 :(得分:0)

请问您能否隔离数据源?

这将澄清你的想法和问题。一旦你有一个服务/存储库来查询数据源,你可以利用linq并做一些简单的事情:

var sum = datasource.Sum(p=>p.AmountPaid);