2011-10-06 67 views
5

对不起,先前的代码。任何人都可以请帮忙? 我有一个GridView GridView1这是由从具有三列Excel表格导入上PageLoad()填充:无法从gridview中的templateField中的文本框中获取值

  1. 日期
  2. 客户
  3. 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()); 

} 

请让我知道如果任何人有一个解决方案。

谢谢。

+0

请清理你的代码示例...太多编辑方式 – CAbbott

+0

嗨CAbbott,对不起,我是这个网站的新手。我试图编辑这个问题,但它变得更加混乱。请给我几分钟,我会解决它。 – Amol

+0

如果它真的是一个按钮'btn',不要调用你的按钮'txtSubmit'或者任何预装了'txt'的东西,它可能会非常具有误导性。你也应该使用'using'语句来包装你的连接对象,这样你就不必显式地调用'.dispose()'。 – JonH

回答

0

在您的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(); 
    } 
} 
0

就个人而言,我会改变你的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()。命名约定和其他小的源代码更改现在不会花你很多时间来修复,但是如果在几周或几个月的其他项目工作之后必须重新访问此代码,将会为您节省大量时间。

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

End Sub 
相关问题