對不起,先前的代碼。任何人都可以請幫忙? 我有一個GridView GridView1
這是由從具有三列Excel表格導入上PageLoad()
填充:無法從gridview中的templateField中的文本框中獲取值
- 日期
- 客戶
- PayingBookNoOrDD
片具有五行。用戶可以編輯頁面文本框中的數據(下面的標記)。編輯完成後,當用戶點擊提交按鈕時,我需要從所有文本框中獲取這些新值,並從GridView
填充位置更新相同的Excel工作表。
我創建了三個字符串數組:dateArray
,custArray
和payingInBookArray
來存儲這些新值。但是當我運行應用程序時,所有三個數組都是空的。
標記:
<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());
}
請讓我知道如果任何人有一個解決方案。
謝謝。
請清理你的代碼示例...太多編輯方式 – CAbbott
嗨CAbbott,對不起,我是這個網站的新手。我試圖編輯這個問題,但它變得更加混亂。請給我幾分鐘,我會解決它。 – Amol
如果它真的是一個按鈕'btn',不要調用你的按鈕'txtSubmit'或者任何預裝了'txt'的東西,它可能會非常具有誤導性。你也應該使用'using'語句來包裝你的連接對象,這樣你就不必顯式地調用'.dispose()'。 – JonH