C# 无法从gridview内templateField中的文本框中获取值
对不起,刚才的代码。有人能帮忙吗? 我有一个gridviewC# 无法从gridview内templateField中的文本框中获取值,c#,asp.net,gridview,textbox,templatefield,C#,Asp.net,Gridview,Textbox,Templatefield,对不起,刚才的代码。有人能帮忙吗? 我有一个gridviewGridView1,它通过从Excel工作表导入而填充在PageLoad()上,该工作表有三列: 日期 顾客 付账单 这张纸有五行。用户可以在页面的文本框中编辑数据(下面的标记)。编辑后,当用户单击submit按钮时,我需要从所有文本框中获取这些新值,并从填充GridView的位置更新相同的Excel表 我创建了三个字符串数组:dateArray、custArray和payingbookarray来存储这些新值。但是当我运行应用程序时,
GridView1
,它通过从Excel工作表导入而填充在PageLoad()
上,该工作表有三列:
GridView
的位置更新相同的Excel表
我创建了三个字符串数组:dateArray
、custArray
和payingbookarray
来存储这些新值。但是当我运行应用程序时,所有三个数组都是空的
标记:
<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());
}
日期
客户
付账单
代码隐藏:
<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());
}
受保护的无效页面加载(对象发送方,事件参数e)
{
string selectQuery=“从[Month1$B2:D5]中选择*”;
OLEDB连接conn=新的OLEDB连接(connString);
conn.Open();
OleDbDataAdapter da=新的OleDbDataAdapter(选择查询,连接);
数据集ds=新数据集();
da.填充(ds);
GridView1.DataSource=ds;
GridView1.DataBind();
康涅狄格州关闭();
da.Dispose();
conn.Dispose();
}
受保护的void txtSubmit\u单击(对象发送方,事件参数e)
{
IList DateArray=新列表();
IList custary=新列表();
IList payInBookArray=新列表();
foreach(GridView1.Rows中的GridViewRow gr)
{
TextBox lblDate=(TextBox)gr.Cells[0]。FindControl(“txtDate”);
DateArray.Add(lblDate.Text);
TextBox lblCustomers=(TextBox)gr.Cells[1]。FindControl(“txtCustomers”);
添加(lblCustomers.Text);
TextBox lblPayInBookNo=(TextBox)gr.Cells[2].FindControl(“txtpayingingbooknoordd”);
payInBookArray.Add(lblPayInBookNo.Text);
}
ExportToExcel(DateArray.ToArray(),custArray.ToArray(),payInBookArray.ToArray());
}
如果有人有解决办法,请告诉我
谢谢。在页面加载事件中添加回发检查。我看不出你的btn_提交代码有什么问题
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();
}
}
就我个人而言,我会将您的
txtSubmit\u 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());
}
protectedvoid txtSubmit\u单击(对象发送方,事件参数e)
{
IList DateArray=新列表();
IList custary=新列表();
IList payInBookArray=新列表();
foreach(GridView1.Rows中的GridViewRow gr)
{
TextBox lblDate=(TextBox)gr.FindControl(“txtDate”);
DateArray.Add(lblDate.Text);
TextBox lblCustomers=(TextBox)gr.FindControl(“txtCustomers”);
添加(lblCustomers.Text);
TextBox lblPayInBookNo=(TextBox)gr.FindControl(“txtpayingingbooknoordd”);
payInBookArray.Add(lblPayInBookNo.Text);
}
ExportToExcel(DateArray.ToArray(),custArray.ToArray(),payInBookArray.ToArray());
}
我在尝试直接访问.Cells
集合中的值时总是遇到问题。当您对行本身调用.FindControl
时会发生什么
正如其他人所说,值得考虑为HTML字段和变量使用新名称。现在使用
IList
类型的DateArray
似乎很简单,但它让我很快地看到了DateArray.ToArray()
。命名约定和其他小的源代码更改现在不会占用您很多时间来修复,但会在您在其他项目上工作数周或数月后必须重新访问此代码时为您节省大量时间。请清理您的代码示例…对于Editi CAbbott来说太多了,很抱歉,我是这个网站的新手。我试图编辑这个问题,但它变得更加混乱。请给我几分钟,我会解决它。不要按按钮txtsmit
或任何前面有txt
的按钮。如果它真的是按钮btn
,那可能会产生误导。您还应该使用using
语句包装连接对象,这样您就不必显式调用.dispose()
。
Protected Sub txtNombres_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Session("FiltroNombres") = DirectCast(sender, TextBox).Text
End Sub