【问题标题】:My TextChanged event on a dynamically created textbox in a custom BoundField never fires?我在自定义 BoundField 中动态创建的文本框上的 TextChanged 事件永远不会触发?
【发布时间】:2013-04-07 08:58:23
【问题描述】:

我正在尝试为我的自定义 GridView 创建自定义 BoundField(列)。我在FooterRow 中添加了文本框来管理对列的过滤。它显示良好,但从未引发 TextChanged 事件。我想这是因为文本框在每次回发时都会重新创建,而不是持久化。

这是我的代码:

public class Column : BoundField
{
    public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
    {
        base.InitializeCell(cell, cellType, rowState, rowIndex);
        if (cellType == DataControlCellType.Footer)
        {
            TextBox txtFilter = new TextBox();
            txtFilter.ID = Guid.NewGuid().ToString();
            txtFilter.Text = "";
            txtFilter.AutoPostBack = true;
            txtFilter.TextChanged += new EventHandler(txtFilter_TextChanged);
            cell.Controls.Add(txtFilter);
        }
    }

    protected void txtFilter_TextChanged(object sender, EventArgs e)
    {
        // Never get here
    }
}

我尝试了一个复选框,它起作用了。

【问题讨论】:

  • 是winforms还是asp.net?
  • 您可以发布自己的答案 - 并将其标记为已接受
  • 您不得更改任何控件的 Id 属性,以免其无法正常工作。在编码中不应该改变

标签: c# asp.net events boundfield


【解决方案1】:

我在 WPF 应用程序中遇到了同样的问题。这样对我来说简直就是工作,

 TextBox txtBx = new TextBox();
 txtBx.Width = 300;
 txtBx.TextChanged += txtBox_TextChanged;

它会调用,

private void txtBox_TextChanged(object sender, EventArgs e)
    {
        errorTxt.Text = "Its working";
    }

“errorTxt”是一个预定义的TextBlock。希望这会对某人有所帮助..

【讨论】:

    【解决方案2】:

    解决方案:

    我终于找到了问题,但我不明白! 问题出在使用 Guid 生成的 ID 属性上。只是删除它解决了我的问题。

    public class Column : BoundField
    {
        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);
            if (cellType == DataControlCellType.Footer)
            {
                TextBox txtFilter = new TextBox();
                // Removing this worked
                //txtFilter.ID = Guid.NewGuid().ToString(); 
                txtFilter.Text = "";
                txtFilter.AutoPostBack = true;
                txtFilter.TextChanged += new EventHandler(txtFilter_TextChanged);
                cell.Controls.Add(txtFilter);
            }
        }
    
        protected void txtFilter_TextChanged(object sender, EventArgs e)
        {
            // Never get here
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 2015-07-26
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 2020-02-01
      相关资源
      最近更新 更多