1

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.

Servy
  • 202,030
  • 26
  • 332
  • 449
Guillaume Martin
  • 103
  • 7
  • 18

2 Answers2

1

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

SilentCoder
  • 1,970
  • 1
  • 16
  • 21
0

Solution:

I finally found the problem, but I don't understand it ! The problem was the ID property, generated with a Guid. Just removing it solved my problem.

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
    }
}
Guillaume Martin
  • 103
  • 7
  • 18