我正在编写ASP.NET C#中的调查程序。我有56个不同的dropdownList控件映射到56个标签控件。我不想通过常规方式列表来映射它:
lblSummary1.Text = DropDownList1.SelectedValue;
lblSummary2.Text = DropDownList2.SelectedValue;
lblSummary3.Text = DropDownList3.SelectedValue;
.
.
lblSummary56.Text = DropDownList56.SelectedValue;
所以我决定以这样的方式编写它:
// Map all the dropDown Selected Data to the their Various Labels
string mylbl = "lblSummary";
string myddl = "DropDownList";
int counter;
int totalAnswers = 57;
for (counter = 1; counter < totalAnswers; counter++ )
{
mylbl += counter.ToString() + "." + "Text;<br/>";
myddl += counter.ToString() + "." + "Text;<br/>";
mylbl = myddl;
}
为了查看输出,我将答案映射到如下标签:
lblSummary57.Text = myddl;
这是我得到的结果
DropDownList1.Text;
2.Text;
3.Text;
4.Text;
5.Text;
6.Text;
7.Text;
8.Text;
.
.
.
56.Text;
请帮助我,我是ASP.NET的新手
谢谢。
答案 0 :(得分:1)
您可以使用FindControl方法动态获取其ID的标签或下拉列表。你可以使用容器代替Page而且速度很快。
Page.FindControl("DropDownList" + i);
你可以通过类似的东西来阅读下拉和标签,
for (counter = 1; counter < totalAnswers; counter++ )
{
DropDownList ddl = (DropDownList ) Page.FindControl("DropDownList" + counter);
Label lblSummary = (Label ) Page.FindControl("lblSummary" + counter);
lblSummary.Text = ddl .SelectedValue;
}
答案 1 :(得分:0)
for (counter = 1; counter < totalAnswers; counter++ )
{
mylbl = "lblSummary" + counter.ToString() + "." + "Text";
myddl = "DropDownList" + counter.ToString() + "." + "SelectedValue";
mylbl = myddl;
}