我正在尝试为自定义BoundField
制作自定义GridView
(列)。我将文本框添加到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
}
}
我尝试了一个复选框,但它确实有效。
答案 0 :(得分: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。希望这会对某人有所帮助..
答案 1 :(得分:0)
<强>解决方案:强>
我终于找到了问题,但我不明白! 问题是使用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
}
}