Page.Findcontrol在C#ASP.Net中不起作用

Page.Findcontrol在C#ASP.Net中不起作用

问题描述:

我获得Page.FindControl的空值:

我有一个从母版页派生的网页.

然后我动态创建了下拉列表和文本框
/>
Table myTable = new Table();
configMatrixPlaceHolder.Controls.Add(myTable);
for(int row = 0; row< = noOfConfigurations; row ++)
{
TableRow rw = new TableRow();
TableHeaderRow headerRow = new TableHeaderRow();柱++){

TableCell的小区1 =新的TableCell();如果
(行== 0){
如果
(列== 0)
{
cell1.Text = QUOT;配置英寸;
}
别的
{
cell1.Text =(startYear +柱)的ToString();}

headerRow.Cells.Add(小区1);
myTable.Controls.Add(headerRow);
}
别的
{
如果(列== 0){

TableCell的细胞=新的TableCell();
DropDownList的configDropDownList =新的DropDownList();
configDropDownList.ID ="configDropDown"; + row.ToString();
响应.Write(configDropDownList.ID); cell.Controls.Add(configDropDownList);
rw.Cells.Add(细胞);
myTable.Controls.Add(RW);
}
别的
{
的TableCell细胞=新的TableCell();文本框
文本=新文本框();
text.Text = QUOT;英寸;
text.ID = QUOT;的textBox&QUOT; + row.ToString()+ column.ToString(); );
}
使用FindControl的下拉列表和文本框我得到的是空值
我用来传递代码的代码是:

DropDownList dropDown =(DropDownList)Page.FindControl("configDropDown" + row.ToString ());
TextBox tBox =(TextBox)FindControl(" textBox" + row.ToString()+ column.ToString());


请帮帮我! !!

I am getting null value for Page.FindControl:

i have webpage which is derived from a master page.

then i create dropdownlists and text boxes dynamically

Table myTable = new Table();
            configMatrixPlaceHolder.Controls.Add(myTable);
            for (int row = 0; row <= noOfConfigurations; row++)
            {
                TableRow rw = new TableRow();
                TableHeaderRow headerRow = new TableHeaderRow();


                for (int column = 0; column <= noOfYears; column++)
                {
                    TableCell cell1 = new TableCell();
                    if (row == 0)
                    {
                        if (column == 0)
                        {
                            cell1.Text = "Configurations";
                        }
                        else
                        {
                            cell1.Text = (startYear + column).ToString();
                        }
                        headerRow.Cells.Add(cell1);
                        myTable.Controls.Add(headerRow);
                    }
                    else
                    {
                        if (column == 0)
                        {
                            TableCell cell = new TableCell();
                            DropDownList configDropDownList = new DropDownList();
                            configDropDownList.ID = "configDropDown" + row.ToString();
                            Response.Write(configDropDownList.ID);
                            //string dropDownName = text.ID.ToString();
                            populateConfigDropDown(configDropDownList);
                            cell.Controls.Add(configDropDownList);
                            rw.Cells.Add(cell);
                            myTable.Controls.Add(rw);
                        }
                        else
                        {
                            TableCell cell = new TableCell();
                            TextBox text = new TextBox();
                            text.Text = "";
                            text.ID = "textBox" + row.ToString()+column.ToString();
                            cell.Controls.Add(text);
                            rw.Cells.Add(cell);
                            myTable.Controls.Add(rw);
                        }
                    }
                }
            }
       



when i try to  access these dynamically created dropdown and text boxes using FindControl i am getting null values
the code i am using to reteive is:

 DropDownList dropDown = (DropDownList)Page.FindControl("configDropDown" + row.ToString());
TextBox tBox = (TextBox)FindControl("textBox" + row.ToString() + column.ToString());


Please help me!!!

我记得,FindControl不会遍历控件树.您正在将控件添加为单元的子级,并且需要以相同的方式找到它们.
As I recall, FindControl does not traverse the control tree.  You are adding the controls as children of cell and will need to find them the same way.