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
问问题
658 次
3 回答
2
这似乎是执行顺序的问题。请记住,直到单击事件之后才会添加控件。因此,当您的按钮单击触发时,需要重新添加控件,然后才能检查它们是否存在。
(我会将此作为评论发布,但显然,由于我是新手,我没有足够的积分)
于 2012-11-01T14:54:29.703 回答
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;
}
于 2012-11-01T14:49:46.157 回答
0
您只是忘记将表格本身添加到页面的控件集合中。
Page.Controls.Add(table);
(最好将其添加到容器控件中,例如PlaceHolder
或Panel
代替)
于 2012-11-01T14:52:46.273 回答