动态创建的文本框的ontextchanged事件在分配ID时不会触发

时间:2013-02-13 04:11:55

标签: c# asp.net

我阅读了所有相关的帖子,但仍然无法理解我目前面临的问题。在Oninit上我创建了带有文本框的表,如下所示,当我为文本框分配一个id时,textchanged事件不会触发,但是我没有指定它发射的ID。无法理解为什么。请帮助。

    protected override void OnInit(EventArgs e)
    {

    PopulateTextBoxes();
    base.OnInit(e);
    }
protected void PopulateTextBoxes()
{
    int quantityRequired = 0;
    quantityRequired =GetQuantity();
    for (int j = 0; j < quantityRequired; j++)
    {
        TableRow row = new TableRow();
        TableCell cell1 = new TableCell();
        TextBox tb = new TextBox();
        //tb.ID = j.ToString() +":RowTbx";---> this when uncommented does not fire my      tb_textChanged event while left commented the textchanged event is fired. Cant understand why? How is assigning an ID affect event Firing, I need to have the ID to further update my Db.                    
        tb.AutoPostBack = true;
        tb.TextChanged += new EventHandler(tb_TextChanged);
        cell1.Controls.Add(tb);
        row.Cells.Add(cell1);
        TableCell cell2 = new TableCell();
        CheckBox chBox = new CheckBox();
        chBox.CheckedChanged += new EventHandler(chBox_CheckedChanged);
        chBox.AutoPostBack = true;
        cell2.Controls.Add(chBox);
        row.Cells.Add(cell2);
        TableCell cell3 = new TableCell();
        Image img = new Image();
        img.Width = Unit.Pixel(25);
        img.Height = Unit.Pixel(25);
        img.ImageUrl = "HttpRuntime.AppDomainAppVirtualPath" + "/Images/" +"img.jpeg";
        cell3.Controls.Add(img);
        row.Cells.Add(cell3);
        tbl_Serial.Rows.Add(row);
    }
}
private void tb_TextChanged(object sender, EventArgs e)
{
    // TODO: Implement this method
    //throw new NotImplementedException();
}

谢谢

2 个答案:

答案 0 :(得分:2)

您设置的ID不是有效的控件ID。

参考:Control.ID

  

仅包含字母数字字符和下划线的组合   character(_)是此属性的有效值。包括空格   或其他无效字符将导致ASP.NET页面解析器错误。

试试这个:

tb.ID = "RowTbx" + j;

答案 1 :(得分:1)

不要将:用于ID。试试这个

tb.ID = j.ToString() + "_RowTbx";