c# - 如何以编程方式将未知数量的下拉列表添加到页面并在提交时使用 C# 检索选定的值

标签 c# asp.net

我正在尝试这样做,这让我有点困惑。

基本上情况是这样的,我从第 3 方应用程序获取 XML,其中包含可用的预订日期,每天都有人可以选择的房间类型,单人间、双人间等。

每家旅馆都会返回我未知数量的房型。但是不要对此感到困惑。

事情很简单,我只需要添加未知数量的下拉列表(或 HTML 选择)以及要预订的人数。现在,因为我不知道我将拥有多少个下拉菜单,所以我需要以编程方式将它们添加到“for int i=0; i

如何以编程方式向页面添加未知数量的下拉列表,并在提交时使用 C# 检索选定的值?

截图最后一列

http://i.stack.imgur.com/37chw.png

更新:

我正在将 xml 结果中的代码创建为将打印为 html 代码的字符串:

 XmlDocument xmlDoc2 = new XmlDocument();
    xmlDoc.LoadXml(getPrices());
    XmlNodeList prices = xmlDoc.GetElementsByTagName("RoomType");

    string[] bookingDates = new string[Convert.ToInt32(Request.QueryString["nights"])];
    string[] bookingDays = new string[Convert.ToInt32(Request.QueryString["nights"])];
    bookingDates[0] = Request.QueryString["date"].ToString();

    string[] dateArray = Request.QueryString["date"].ToString().Split('-');
    DateTime initialDate = new DateTime(Convert.ToInt32(dateArray[0]), Convert.ToInt32(dateArray[1]), Convert.ToInt32(dateArray[2]));
    bookingDays[0] = initialDate.DayOfWeek.ToString();
    for (int z = 1; z < bookingDates.Length; z++)
    {
        DateTime nextDay = initialDate.AddDays(z);
        string month = nextDay.Month.ToString();
        string day = nextDay.Day.ToString();
        if (day.Length == 1)
        {
            day = "0" + day;
        }
        if (month.Length == 1)
        {
            month = "0" + month;
        }
        bookingDates[z] = nextDay.Year + "-" + month + "-" + day;
        bookingDays[z] = nextDay.DayOfWeek.ToString();
    }
    string pricesHeader = "<table width='100%'>";
    pricesHeader += "<tr><td>Room Type</td>";
    for (int x = 0; x < bookingDates.Length; x++)
    {
        string[] bookingDay = bookingDates[x].Split('-');
        pricesHeader += "<td align='center'>" + bookingDays[x].Substring(0, 3) + "<br>" + bookingDay[2] + "</td>";
    }
    pricesHeader += "<td>Persons</td></tr>";
    string pricesContent = "<tr>";
    int dropNumber = 1;
    foreach (XmlElement node in prices)
    {
        XmlNodeList roomTypeDescriptionN = node.GetElementsByTagName("roomTypeDescription");
        string roomTypeDescriptionS = roomTypeDescriptionN[0].InnerText;
        pricesContent += "<td>" + roomTypeDescriptionS + "</td>";
        XmlNodeList priceN = node.GetElementsByTagName("price");
        string priceS = priceN[0].InnerText;
        XmlNodeList currencyN = node.GetElementsByTagName("currency");
        string currencyS = currencyN[0].InnerText;
        if (currencyS == "EUR")
        {
            currencyS = "&euro";
        }
        string avDates = "";
        XmlNodeList availableDatesN = node.GetElementsByTagName("date");
        int dateNumber = 0;
        foreach (XmlElement avDate in availableDatesN)
        {
            avDates += availableDatesN[dateNumber].InnerText + ",";
            dateNumber++;
        }

        for (int c = 0; c < bookingDates.Length; c++)
        {
            if (avDates.Contains(bookingDates[c]))
            {
                pricesContent += "<td>" + priceS + currencyS + "</td>";
            }
            else
            {
                pricesContent += "<td><center>X</center></td>";
            }
        }
        pricesContent += "<td><select runat=server name='pers" + dropNumber + "' id='pers" + dropNumber + "'>" +
                      "<option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option></select></td></tr>";
        dropNumber++;
    }
    pricesLabel.Text = pricesHeader + pricesContent + "</table>";

我知道这样做并添加 runat=server 对我的控制没有帮助,现在我的主要问题是,如何在 html 上添加代码以便稍后能够使用下拉列表选择值C#。我可以用 Request.Form 做到这一点吗?正在尝试,但到目前为止我做不到。

最佳答案

您可以使用 Repeater Class用于生成控件
您还可以使用 Request.Form Collection用于获取用户的选择

关于c# - 如何以编程方式将未知数量的下拉列表添加到页面并在提交时使用 C# 检索选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4286319/

相关文章:

c# - FluentNHibernate 无法从 web.config 中读取 connectionString

asp.net - SQL Server主-子关系而不是表中的存储过程

c# - WPF TextBox 被切断

Asp.net 用户控件负载控制问题

c# - LINQ to SQL - 自定义属性

c# - System.Text.Json:如何使用接口(interface)属性反序列化类(.NET 6)

asp.net - 通过局域网连接到 Visual Studio 调试 IIS Express 服务器

c# - 使用C#登录网站连接现有的sql server db

c# - 是什么导致 "The type or namespace name ' UI' does not exist in the namespace"错误?

c# - 下拉列表值重复