2011-10-06 88 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 
相關問題