无法从 gridview 内的 templateField 中的文本框中获取值
对之前的代码感到抱歉。有人可以帮忙吗? 我有一个 gridview GridView1
,通过从具有三列的 Excel 工作表导入来填充 PageLoad()
:
- Date
- Customers
- 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());
}
如果有人有解决方案,请告诉我。
谢谢。
Sorry about the code earlier. Could anyone please help?
I have a gridview GridView1
which is populated on PageLoad()
by importing from Excel sheet that has three columns:
- Date
- Customers
- PayingBookNoOrDD
The sheet has five rows. Users can edit the data in textboxes on the page (markup below). After editing, when the user clicks on the submit button, I need to get these new values from all the textboxes and update the same Excel sheet from where the GridView
was populated.
I created three string arrays: dateArray
, custArray
, and payingInBookArray
to store these new values. But when I run the application all three arrays are empty.
Markup:
<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" />
Code-behind:
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());
}
Please let me know if anyone has a solution.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
添加对 Page_Load 事件的回发检查。我看不出您的 btn_Submit 代码有任何问题。
Add a postback check on your Page_Load event. I can't see anything wrong with your btn_Submit code.
就我个人而言,我会将您的 txtSubmit_Click 函数更改为:
我在尝试直接访问 .Cells 集合中的值时总是遇到问题。当您对行本身调用
.FindControl
时会发生什么?正如其他人所说,值得考虑 HTML 字段和变量的新名称。现在看起来,拥有
IList
类型的DateArray
似乎微不足道,但它短暂地让我陷入了循环查看DateArray.ToArray()
。命名约定和其他小的源代码更改现在不会花费您很多时间来修复,但是当您在其他项目上工作数周或数月后必须重新访问此代码时,将为您节省大量时间。Personally, I would change your
txtSubmit_Click
function to this:I've always had problems trying to directly access values in the
.Cells
collection. What happens when you call.FindControl
on the row itself?As others have said, it's worth thinking about new names for your HTML fields and your variables. It seems trivial now to have
DateArray
of typeIList
, but it briefly threw me for a loop to seeDateArray.ToArray()
. Naming conventions and other small source code changes won't take you a lot of time to fix now, but will save you plenty of time later when you have to revisit this code after weeks or months of working on other projects.尝试使用以下方法收集值
foreach (GridViewRow gr in GridView1.Rows)
{
string date= ((TextBox)gr.FindControl("txtDate")).text;
}
try using the following method to collect the value
foreach (GridViewRow gr in GridView1.Rows)
{
string date= ((TextBox)gr.FindControl("txtDate")).text;
}