0

我有一个正在编写的 ASP.NET C# 调查程序。我有 56 个不同的下拉列表控件要映射到 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 的新手

谢谢你。

4

2 回答 2

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;
}
于 2013-10-28T11:52:21.707 回答
0
for (counter = 1; counter < totalAnswers; counter++ )
{
    mylbl = "lblSummary" + counter.ToString() + "." + "Text";
    myddl = "DropDownList" + counter.ToString() + "." + "SelectedValue";
    mylbl = myddl;
}
于 2013-10-28T11:58:37.977 回答