【问题标题】:Gridview Update Returning nullGridview更新返回null
【发布时间】:2011-08-22 12:21:32
【问题描述】:

我正在尝试对 gridview 使用更新方法,它只是返回 null

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox Currency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Currency"));
        GridView1.EditIndex = -1;

        if (!Page.IsPostBack)
        {
            FillTravelers();
        }
    }

有什么想法吗?当我按下更新时,它只是将字段返回到之前的值,但什么也不返回。

谢谢

【问题讨论】:

  • 请发布 FillTravelers() 方法体。什么返回 null ?请注明。
  • fillTravelers 是一种仅绑定数据的方法,问题在于 TextBox Currency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Currency"));货币返回 null
  • 好的。你确定你的 GridView 有名为“货币”的文本框吗?
  • 这就是我的想法,但我不确定如何检查出来,但是列名肯定是货币,当我用鼠标浏览文本框时,它会显示货币,是否存在我可以检查一下的更好方法吗?
  • 您应该可以从 .aspx 文件中看到 TextBox 的 ID。它应该类似于<asp:TextBox ID="Currency" runat="server" />

标签: c# asp.net visual-studio-2010 gridview


【解决方案1】:

由于您的 Gridview 是最简单的形式,在 gridview 中没有定义任何控件(因此 GridView 中没有定义货币文本框),请检查 GridView1 的数据源,我假设它是一个数据表。 然后你需要确定Datatable的哪一列是Currency列。

例如,对于下面的数据表,它将是第 2 列。

  DataTable taskTable = new DataTable("AmountList");
  taskTable.Columns.Add("Id", typeof(int));
  taskTable.Columns.Add("Currency", typeof(string));
  taskTable.Columns.Add("Amount", typeof(decimal)); 

基于此,您可以使用以下代码检索更新后的货币值。

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = TaskGridView.Rows[e.RowIndex];
        GridView1.EditIndex = -1;
        TextBox txtCurrency = (TextBox)(row.Cells[2].Controls[0]);
        if (!Page.IsPostBack)
        {
            FillTravelers();
        }
        //... Rest of the code
    }

请注意,如果上面的 Currency 列不同,那么您也需要更改 rows.Cells[index].Controls[0] 的索引。例如,如果 Currency 在第 5 列,则 txtCurrency 定义变为:

 TextBox txtCurrency = (TextBox)(row.Cells[5].Controls[0]);

如果您无法轻松找出数据表的列索引(或者如果您使用的是更复杂的数据源),那么,假设 GridView1 没有太多列,我建议您尝试增加 Cells逐步索引,直到在调试时看到 Watch for row.Cells[index].Controls[0] 中的更新值。

编辑:(添加代码以检索货币列的列索引) 您可以将“货币”作为字符串和 OracleDataReader 实例传递给下面的方法,这应该会为您提供列索引(或者如果 OracleDataReader 中没有货币列,它会为您提供 -1)。

private int GetColumnIndexIfExists(OracleDataReader dr, string columnName)
{
    for (int i = 0; i < dr.FieldCount; i++)
    {
        if (dr.GetName(i).Equals(columnName))
        {
            return i;
        }
    }
    return -1;
}

然后在实际代码中,您可以修改单元格索引加1,如下所示,因为单元格索引不像列索引那样基于零。

TextBox txtCurrency = (TextBox)(row.Cells[i+1].Controls[0]);

【讨论】:

  • 感谢您的回复,但我没有使用数据表,而是使用 OracleDataReader,这个问题仍然困扰着我,不知道我还能做什么:S
  • 它应该与 SQLDataReader 比较相似,您可以在其中检索 Currency 列的 columnindex。我将编辑答案以包含代码。
  • 您仍然可以尝试在 GridView1_RowUpdating 中使用断点调试并逐步将单元格编号增加 1 的试错法(如果您没有太多列)并观察每个单元格的值直到您获得更新的货币值。我假设 OracleDataReader 中列的顺序不会改变。
  • 没问题,问题解决了 TextBox txtCurr = ((TextBox)(row.Cells[3].Controls[0]));
【解决方案2】:

我不确定你是否分享了足够的细节,但我最初的想法是,如果你的“更新”涉及按下按钮(因此,回发) - 你的 filltravelers 不会被触发,因为 !Page .IsPostback 将是错误的。

我在这里假设您的方法“FillTravelers”在您点击更新时具有处理过滤的范围。

【讨论】:

  • 但我认为这不是问题所在,当我调试并阅读该字段时,它也会返回 null ......这是在 fillTravelers 之前
  • 我认为需要更多细节以及一些额外的实际代码 - 无法看到问题出在哪里,因此很难提供任何进一步的建议。
  • protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { TextBox Currency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Currency"));会不会有问题?
  • 它可能是 - FindControl 是否真的找到了文本框,或者它是否返回 null。如果它返回 null,您需要调整为它的子控件询问控件的方式,因为它没有找到它。
  • 它似乎没有找到文本框,我认为文本框 id 是错误的,但我不确定,因为这是一个更新,而不是在 aspx 中声明的普通文本框
【解决方案3】:

由于没有任何TextBox 控件名为Currency,因此FindControl() 方法的结果为null。试试这个,

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   // Here you get the row that you want to edit.
   GridViewRow row = GridView1.Rows[e.RowIndex];
   // This one returns you the cell inside the row that is being edited.
   // Replace 1 with the column no you want.
   TableCell tableCell = row .Controls[1] as TableCell;
   // And finally you have your textbox.
   TextBox textBox = tableCell.Controls[0] as TextBox;        
}

【讨论】:

  • 你试过调试吗?也许你正试图弄错 tableCell。
猜你喜欢
  • 1970-01-01
  • 2012-10-31
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 2017-04-06
  • 2010-10-05
相关资源
最近更新 更多