无法从gridview内的templateField的文本框中获取值

本文关键字:文本 获取 templateField gridview 内的 | 更新日期: 2023-09-27 18:10:48

很抱歉之前的代码。有人能帮忙吗?我有一个gridview GridView1,它通过从有三列的Excel表导入PageLoad()来填充:

  1. 客户
  2. PayingBookNoOrDD

工作表有五行。用户可以在页面上的文本框中编辑数据(下面有标记)。编辑后,当用户单击提交按钮时,我需要从所有文本框中获取这些新值,并从填充GridView的地方更新相同的Excel工作表。

我创建了三个字符串数组:dateArraycustArraypayingInBookArray来存储这些新值。但是当我运行应用程序时,这三个数组都是空的。

标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Date,Customers,PayingInBookNoOrDD" >
    <Columns>
    <asp:TemplateField>
        <HeaderTemplate>Date</HeaderTemplate>
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtDate" Text='<%# Bind("Date") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <HeaderTemplate>Customers</HeaderTemplate>
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtCustomers" Text='<%# Bind("Customers") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <HeaderTemplate>PayingInBookNoOrDD</HeaderTemplate>
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtPayingInBookNoOrDD" Text='<%# Bind("PayingInBookNoOrDD") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button ID="txtSubmit" runat="server" Text="Submit" onclick="txtSubmit_Click" />

后台代码:

protected void Page_Load(object sender, EventArgs e)
{
    string selectQuery = "SELECT * FROM [Month1$B2:D5]";
    OleDbConnection conn = new OleDbConnection(connString);
    conn.Open();
    OleDbDataAdapter da = new OleDbDataAdapter(selectQuery, conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    conn.Close();
    da.Dispose();
    conn.Dispose();
}
protected void txtSubmit_Click(object sender, EventArgs e)
{
    IList<string> DateArray = new List<string>();
    IList<string> custArray = new List<string>();
    IList<string> payInBookArray = new List<string>();
    foreach (GridViewRow gr in GridView1.Rows)
    {
        TextBox lblDate = (TextBox)gr.Cells[0].FindControl("txtDate");
        DateArray.Add(lblDate.Text);
        TextBox lblCustomers = (TextBox)gr.Cells[1].FindControl("txtCustomers");
        custArray.Add(lblCustomers.Text);
        TextBox lblPayInBookNo = (TextBox)gr.Cells[2].FindControl("txtPayingInBookNoOrDD");
        payInBookArray.Add(lblPayInBookNo.Text);
    }
    ExportToExcel(DateArray.ToArray(), custArray.ToArray(), payInBookArray.ToArray()); 
}

如果有人有解决办法,请告诉我。

谢谢。

无法从gridview内的templateField的文本框中获取值

在Page_Load事件上添加回发检查。我看不出你的btn_Submit代码有什么问题。

protected void Page_Load(object sender, EventArgs e)
{
    if(!this.IsPostBack){
         string selectQuery = "SELECT * FROM [Month1$B2:D5]";
         OleDbConnection conn = new OleDbConnection(connString);
         conn.Open();
         OleDbDataAdapter da = new OleDbDataAdapter(selectQuery, conn);
         DataSet ds = new DataSet();
         da.Fill(ds);
         GridView1.DataSource = ds;
         GridView1.DataBind();
         conn.Close();
         da.Dispose();
         conn.Dispose();
    }
}

就我个人而言,我会将您的txtSubmit_Click函数更改为:

protected void txtSubmit_Click(object sender, EventArgs e)
{
    IList<string> DateArray = new List<string>();
    IList<string> custArray = new List<string>();
    IList<string> payInBookArray = new List<string>();
    foreach (GridViewRow gr in GridView1.Rows)
    {
        TextBox lblDate = (TextBox)gr.FindControl("txtDate");
        DateArray.Add(lblDate.Text);
        TextBox lblCustomers = (TextBox)gr.FindControl("txtCustomers");
        custArray.Add(lblCustomers.Text);
        TextBox lblPayInBookNo = (TextBox)gr.FindControl("txtPayingInBookNoOrDD");
        payInBookArray.Add(lblPayInBookNo.Text);
    }
    ExportToExcel(DateArray.ToArray(), custArray.ToArray(), payInBookArray.ToArray()); 
}

在试图直接访问.Cells集合中的值时,我总是遇到问题。当对行本身调用.FindControl时会发生什么?

正如其他人所说,为HTML字段和变量考虑新名称是值得的。现在让DateArray的类型为IList似乎微不足道,但是看到DateArray.ToArray()时,它让我陷入了一个循环。命名约定和其他小的源代码更改现在不会花费您很多时间来修复,但是当您在其他项目上工作数周或数月后必须重新访问这些代码时,将为您节省大量时间。

Protected Sub txtNombres_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("FiltroNombres") = DirectCast(sender, TextBox).Text
End Sub

尝试使用以下方法收集值

foreach (GridViewRow gr in GridView1.Rows)

{

string date= ((TextBox)gr.FindControl("txtDate")).text;

}