Amo*_*mol 5 c# asp.net gridview textbox templatefield
对于之前的代码抱歉.有人可以帮忙吗?我有一个gridview GridView1,PageLoad()通过从包含三列的Excel工作表导入填充:
这张纸有五排.用户可以编辑页面上文本框中的数据(下面的标记).编辑后,当用户点击提交按钮时,我需要从所有文本框中获取这些新值,并从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" />
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
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());
}
Run Code Online (Sandbox Code Playgroud)
如果有人有解决方案,请告诉我.
谢谢.
小智 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();
}
}
Run Code Online (Sandbox Code Playgroud)