查找动态添加的文本框的值

Find value of dynamically added textbox

本文关键字:文本 查找 添加 动态      更新时间:2023-09-26

我从一个数据集中生成了一些问题,在动态生成的html表中,这些问题旁边有文本框。我有5个问题和5个文本框。所以,我需要取回一些文本框的值。

我正在循环浏览数据集,并添加了一系列带有文本框的问题。但是,我是否需要为每个文本框生成一些唯一的ID?

private void GenerateTable(int colsCount, int rowsCount, DataSet ds, DataSet ds2)
{
    if (!IsPostBack)
    {
        int counter = 0;
        List<TableRow> tableRowList = new List<TableRow>();
        List<TableCell> tableCellList = new List<TableCell>();
        List<Control> controlsList = new List<Control>();
        foreach (DataTable dataTable in ds.Tables)
        {
            foreach (DataRow dataRow in dataTable.Rows)
            {
                TableRow tableRow = new TableRow();
                TableCell tableCell = new TableCell();
                TableCell tableCell2 = new TableCell();
                TextBox textbox = new TextBox();
                //textbox.ID = "TextBoxID";
                TableRow tableRow2 = new TableRow();
                TableCell tableCel2 = new TableCell();
                tableCell.Text = dataRow["QUESTIONTEXT"].ToString();

                if (dataRow["TYPEID"].ToString() == "1")
                {
                    tableCell2.Controls.Add(textbox);
                    controlsList.Add(textbox);
                }

                tableRow.Cells.Add(tableCell);
                tableRow.Cells.Add(tableCell2);
                myTable.Rows.Add(tableRow);
                tableRowList.Add(tableRow);
                tableCellList.Add(tableCell);
                tableCellList.Add(tableCell2);
                counter++;
            }
        }
        //TextBox tb = myTable.FindControl(TextBoxID) as TextBox;          

我需要为每个文本框生成一些唯一的ID吗?

是的。。。id必须是唯一的。具有多个具有相同id的元素的页面是invaliid

规格:

id=name[CS]
此属性为元素指定名称。此名称在文档中必须是唯一的。

但如果你根本不使用id,你就根本不能分配id。。。