C# 在单元格中找不到任何控件 private void btnSaveStudy\u单击(对象发送方,事件参数e) { string valueFromlbl=string.Empty; for(int i=0;i

C# 在单元格中找不到任何控件 private void btnSaveStudy\u单击(对象发送方,事件参数e) { string valueFromlbl=string.Empty; for(int i=0;i,c#,asp.net,C#,Asp.net,以root用户身份传入页面,查看您要查找的控件是否返回 private void btnSaveStudy_Click(object sender, EventArgs e) { string valueFromlbl = string.Empty; for(int i = 0; i < tableContent.Rows.Count; i++) { for(i

以root用户身份传入页面,查看您要查找的控件是否返回

    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

以根用户身份传入页面,查看您要查找的控件是否返回

    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

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

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;
}

(最好将其添加到容器控件中,如
占位符
面板

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

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;
}

(最好将其添加到容器控件中,如
占位符
面板

这似乎是执行顺序的问题。记住你的骗局