从动态创建的下拉列表中检索值
retrieve values from dynamically created dropdownlist
我正在创建一个 table 并动态添加了一些控件,但是当我用 firebug 检查它们时,我分配给这些控件的 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;
但是没有任何想法?
要在 Firebug 中检查时具有相同的 ID,您应该使用以下之一。
将下拉菜单的客户端模式设置为静态(这样它在客户端将具有相同的 ID。
ddl.ClientIDMode = ClientIDMode.Static;
2.Use ClientId 属性 in javascript 获取控件的确切 ID。
var ddlValue = document.getElementById("<% ddl.ClientID %>");
尝试设置 ClientIDMode = Static
,用于您添加的控件,如 MSDN
中所述
希望对您有所帮助!
我意识到我遇到的问题是在尝试 FindControl() 时我传递的是控件的 ID 而不是控件名称,这解决了我的问题。
我正在创建一个 table 并动态添加了一些控件,但是当我用 firebug 检查它们时,我分配给这些控件的 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;
但是没有任何想法?
要在 Firebug 中检查时具有相同的 ID,您应该使用以下之一。
将下拉菜单的客户端模式设置为静态(这样它在客户端将具有相同的 ID。
ddl.ClientIDMode = ClientIDMode.Static;
2.Use ClientId 属性 in javascript 获取控件的确切 ID。
var ddlValue = document.getElementById("<% ddl.ClientID %>");
尝试设置 ClientIDMode = Static
,用于您添加的控件,如 MSDN
希望对您有所帮助!
我意识到我遇到的问题是在尝试 FindControl() 时我传递的是控件的 ID 而不是控件名称,这解决了我的问题。