我试图为我的自定义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
}
}
我尝试了一个复选框,它工作。
我在WPF应用程序中遇到了同样的问题。它只是像这样为我工作,
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"是一个预定义的TextBlock。希望这将帮助一些人…
解决方案:
我终于找到了问题,但我不明白它!问题在于ID属性,它是由Guid生成的。只要把它去掉,问题就解决了。
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
}
}