My TextChanged event on a dynamically created textbox in a custom BoundField never fires?

Guillaume Martin picture Guillaume Martin · Apr 15, 2013 · Viewed 8.2k times · Source

I'm trying to make a custom BoundField (Column) for my custom GridView. I added textboxes to the FooterRow to manage filtering on columns. It displays well, but the TextChanged event is never raised. I guess it is because the textboxes are recreated on each postback, and not persisted.

Here is my code:

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
    }
}

I tried with a checkbox, and it worked.

Answer

SilentCoder picture SilentCoder · Oct 3, 2014

I had same problem in a WPF app. It was simply work for me like this way,

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

And It calls,

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

"errorTxt" is a pre defined TextBlock. Hope this will help some one..