没有在细胞中找到任何控制

时间:2012-11-01 14:42:02

标签: c# asp.net

    private void btnSaveStudy_Click(object sender, EventArgs e)
        {
            string valueFromlbl = string.Empty;

            for(int i = 0; i < tableContent.Rows.Count; i++)
            {
                for(int j = 0; j < tableContent.Rows[i].Cells.Count; j++)
                {
                    foreach(Control ctrl in tableContent.Rows[i].Cells[j].Controls)
                    {
                        Label lbl = ctrl as Label;
                        if(lbl != null)
                        {
                            valueFromlbl = lbl.Text;
                        }
                    }
                }
            }
        }

protected void Page_Load(object sender, EventArgs e)
{
HtmlTable table = null;
                HtmlTableRow row = null;
                HtmlTableCell cell = null;
                studyNumber = studyNumber + 1;
                uniqueID = uniqueID + 1;

                for(int i = 0; i < 5; i++)
                {
                    table = new HtmlTable();
                    row = new HtmlTableRow();

                    tableContent.Controls.AddAt(i, row);
                    for(int j = 0; j < 3; j++)
                    {
                        cell = new HtmlTableCell();
                        cell.Attributes.Add("Class", "csstablelisttd");
                        row.Attributes.Add("Class", "csstextheader");
                        row.Controls.AddAt(j, cell);

                        if(i == 0 && j == 0)
                        {
                            cell.InnerText = "Study : " + Convert.ToInt32(studyNumber);

                        }
                        else if(i == 1 && j == 0)
                        {
                            cell.InnerText = "Modality" + " : " + modality;
                        }
                        else if(i == 2 && j == 0)
                        {
                            cell.InnerText = "Start Date" + " : " + DateTime.Now.ToString("dd-MMM-yyyy");
                        }
                        else if(i == 3 && j == 0)
                        {
                            cell.InnerText = "Accession Number" + " : " + accessionNumber;
                        }
                        else if(i == 4 && j == 0)
                        {
                            Button btnSaveStudy = new Button();
                            btnSaveStudy.ID = "btnSaveStudy" + uniqueID;
                            btnSaveStudy.Text = "Save";
                            btnSaveStudy.Attributes.Add("Class", "cssbutton");
                            cell.Controls.Add(btnSaveStudy);
                            btnSaveStudy.Click += new EventHandler(btnSaveStudy_Click);
                        }
                        if(i == 1 && j == 1)
                        {
                            cell.InnerText = "AE Title" + " : " + schedule_Station_AE_Title;
                        }
                        else if(i == 1 && j == 2)
                        {
                            cell.InnerText = "Station Name" + " : " + schedule_Station_Name;
                        }
                        else if(i == 2 && j == 1)
                        {
                            cell.InnerText = "Start time" + " : " + startTime;
                        }
                        else if(i == 3 && j == 1)
                        {
                            cell.InnerText = "End time" + " : " + endTime;
                        }

                        else if(i == 2 && j == 2)
                        {
                            Label lblPriority = new Label();
                            lblPriority.ID = "lblPriority" + uniqueID;
                            lblPriority.Text = "Priority : ";
                            DropDownList ddlPriority = new DropDownList();
                            ddlPriority.ID = "ddlPriority" + uniqueID;
                            ddlPriority.Attributes.Add("Class", "csstextbox");
                            ddlPriority.Items.Add(new ListItem("MEDIUM", "4"));
                            ddlPriority.Items.Add(new ListItem("STAT", "1"));
                            ddlPriority.Items.Add(new ListItem("HIGH", "2"));
                            ddlPriority.Items.Add(new ListItem("ROUTINE", "3"));
                            ddlPriority.Items.Add(new ListItem("LOW", "5"));
                            cell.Controls.Add(lblPriority);
                            cell.Controls.Add(ddlPriority);
                        }
                        else if(i == 3 && j == 2)
                        {
                            Label lblStudy = new Label();
                            lblStudy.ID = "lblStudy" + uniqueID;
                            lblStudy.Text = "Study : ";
                            DropDownList ddlStudyList = new DropDownList();
                            ddlStudyList = BindStudy(ddlStudyList, Convert.ToInt32(acqModalityID), uniqueID);
                            ddlStudyList.Attributes.Add("Class", "csstextbox");
                            cell.Controls.Add(lblStudy);
                            cell.Controls.Add(ddlStudyList);
                        }
                    }
                }
            }}
I have added controls to table cell but not find any control

3 个答案:

答案 0 :(得分:2)

这似乎是执行订单的问题。请记住,在点击事件发生之后才会添加控件。因此,当您点击按钮时,需要重新添加控件,然后才能检查它们的存在。

(我会将此作为评论发布,但显然,因为我是新人,我没有足够的分数)

答案 1 :(得分:1)

以root身份传入Page,查看您要查找的控件是否返回

private Control FindControlRecursive(Control rootControl, string controlID)
{
    if (rootControl.ID == controlID) return rootControl;

    foreach (Control controlToSearch in rootControl.Controls)
    {
        Control controlToReturn = 
            FindControlRecursive(controlToSearch, controlID);
        if (controlToReturn != null) return controlToReturn;
    }
    return null;
}

答案 2 :(得分:0)

您只是忘记将表格本身添加到页面的控件集合中。

Page.Controls.Add(table);

(最好将其添加到PlaceHolderPanel等容器控件中