How can I add an unknownn number of dropdownlists programatically to a page and retrieve the selected values with c# on submit
I'm trying to do this and it is a bit confusing for me.
Basically the scenario is like this, I'm getting an XML from a 3rd party application with available dates for booking, for each day there are types of rooms the person can choose, single, double, etc.
Each hostel will return me an unknown number of room types. But dont get too confused with this.
The thing is simple I just need to add an unknown number of dropdownlists (or HTML Select) with the numbers of persons to book for. Now because I don't know how many of those dropdowns I will have I need to add them programatically inside a "for int i=0; i
How can I add an unknownn number of dropdownlists programatically to a page and retrieve the selected values with c# on submit?
The last column on the screenshot
http://i.stack.imgur.com/37chw.png
Update:
I'm creating the code from the xml results as a string that will print as html code:
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)
{
开发者_如何学Go 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>";
I know that doing that and adding the runat=server won't help on my control, there is where my main problem is now, how to add the code on the html to be able to get the dropdownlist selected value later with c#. Can I do that with Request.Form ? was trying but so far I couldnt do it.
You can use the Repeater Class for generation of controls
You can also use the Request.Form Collection for obtaining of user's choice
You can use List for saving your created dropdownlists. On submit, you can read the data from your list.
List<DropDownList> ddlList = new List<DropDownList>{};
for(int i=0;i<count;i++)
{
//add control to page
ddlList.items.add(YourNewlyCreatedDdl);
}
for (int i = 0; i < 5; i++)
{
DropDownList ddl = new DropDownList();
ddl.ID=string.Format("ddl_{0}",i);
this.form1.Controls.Add(ddl);
}
This creates 5 empty DropDownLists.
You will need to rebuild and repopulate the controls on each postback in order for ASP to recognise their values.
精彩评论