无法从 gridview 内的 templateField 中的文本框中获取值

发布于 2024-12-08 16:20:57 字数 2986 浏览 1 评论 0原文

对之前的代码感到抱歉。有人可以帮忙吗? 我有一个 gridview GridView1,通过从具有三列的 Excel 工作表导入来填充 PageLoad()

  1. Date
  2. Customers
  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()); 

}

如果有人有解决方案,请告诉我。

谢谢。

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:

  1. Date
  2. Customers
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风吹短裙飘 2024-12-15 16:20:57

添加对 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();
    }
}

Add a postback check on your Page_Load event. I can't see anything wrong with your btn_Submit code.

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();
    }
}
心欲静而疯不止 2024-12-15 16:20:57

就我个人而言,我会将您的 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 字段和变量的新名称。现在看起来,拥有 IList 类型的 DateArray 似乎微不足道,但它短暂地让我陷入了循环查看 DateArray.ToArray()。命名约定和其他小的源代码更改现在不会花费您很多时间来修复,但是当您在其他项目上工作数周或数月后必须重新访问此代码时,将为您节省大量时间。

Personally, I would change your txtSubmit_Click function to this:

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()); 

}

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 type IList, but it briefly threw me for a loop to see DateArray.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.

病女 2024-12-15 16:20:57
Protected Sub txtNombres_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("FiltroNombres") = DirectCast(sender, TextBox).Text

End Sub
Protected Sub txtNombres_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("FiltroNombres") = DirectCast(sender, TextBox).Text

End Sub
萌无敌 2024-12-15 16:20:57

尝试使用以下方法收集值

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;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文