簡體   English   中英

我在自定義BoundField中動態創建的文本框上的TextChanged事件從不觸發?

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

我試圖為我的自定義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;

它呼吁

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

“ errorTxt”是預定義的TextBlock。 希望這會有所幫助。

解:

我終於找到了問題,但是我不明白! 問題是由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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM