我创建了一个表并且动态地添加了一些控件,但是当我用萤火虫检查它们时,它们似乎分配给这些控件的ID似乎不是相同的,它们似乎得到一个前缀所以试图做FindControls("controlname")
发布页面时,我没有太多的喜悦。控件id的示例:ctl00_ContentPlaceHolder1_Monday_Normal_Small,但随后将该前缀添加到控件名称中也没有多少喜悦。 任何建议将不胜感激,在此先感谢。从动态创建的下拉列表中检索值
protected void Page_Load(object sender, EventArgs e)
{
CreateMenu();
}
public void CreateMenu()
{
Table table = new Table();
table.Attributes.Add("class", "table table-bordered");
Label lbl = new Label();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TableHeaderCell thc = new TableHeaderCell();
lbl.Text = "Day";
thc.Controls.Add(lbl);
tr.Cells.Add(thc);
thc = new TableHeaderCell();
lbl = new Label();
lbl.Text = "Meal";
thc.Controls.Add(lbl);
tr.Cells.Add(thc);
thc = new TableHeaderCell();
lbl = new Label();
lbl.Text = "Normal";
thc.Controls.Add(lbl);
tr.Cells.Add(thc);
thc = new TableHeaderCell();
lbl = new Label();
lbl.Text = "No Carb";
thc.Controls.Add(lbl);
tr.Cells.Add(thc);
table.Rows.Add(tr);
for(int i=0;i<5;i++)
{
tr = new TableRow();
tc = new TableCell();
lbl = new Label();
lbl.Text = "<h1 style='text-align:right;'>" + GetDay(i) + "</h1>";
tc.Controls.Add(lbl);
tr.Cells.Add(tc);
tc = new TableCell();
lbl = new Label();
lbl.Text = GetMeal(i);
tc.Controls.Add(lbl);
tr.Cells.Add(tc);
for (int j = 0; j <= 1; j++)
{
tc = new TableCell();
Table dropdowntable = new Table();
TableRow tr2 = new TableRow();
TableCell tc2 = new TableCell();
for (int k = 0; k <= 2; k++)
{
DropDownList ddl = new DropDownList();
ddl.ID = GetDay(i) + "_" + GetType(j) + "_" + GetSize(k);
ddl.DataSource = numbers;
ddl.DataBind();
tc2.Controls.Add(ddl);
}
tr2.Cells.Add(tc2);
dropdowntable.Rows.Add(tr2);
tc.Controls.Add(dropdowntable);
tr.Cells.Add(tc);
}
table.Rows.Add(tr);
}
tableplaceholder.Controls.Add(table);
}
香港专业教育学院改变了DDL控制的的ClientIDMode并没有前缀,但这似乎没有解决我的问题,所以我有一个在我的页面底部的提交按钮并调用其点击方法尝试这种
DropDownList ddl = (DropDownList)FindControl(controlName);
try
{
int num = Convert.ToInt32(ddl.SelectedValue);
}
catch(Exception ex)
{
return 0;
}
return 0;
但没有运气有任何想法?
使用'ClientIDMode =“Static”'这将避免'前缀' – Webruster