1

我阅读了所有相关的帖子,但仍然无法理解当前面临的问题。在 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();
}

谢谢

4

2 回答 2

2

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

参考:Control.ID

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

试试这个:

tb.ID = "RowTbx" + j;
于 2013-02-13T04:18:16.703 回答
1

不要:用于ID. 试试这个

tb.ID = j.ToString() + "_RowTbx";
于 2013-02-13T04:15:32.743 回答