Need to Retrieve data from dynamically added text boxes - asp.net

I added text boxes dynamically in ASP.Net from server side. By using one example which is shown in followed link
http://www.dotnettips4u.com/2013/03/dynamically-creating-text-boxes-using-c.html.
But I couldn't find out how to retrieve values from those text boxes..
Please help me to come out from this..
Here I am posting my code also..
Client Side Code:
<asp:TextBox ID="NoOfPsngr" runat="server" />
<asp:Button ID="AddP" runat="server" Text="Add Passengers" OnClick="AddP_Click" />
<asp:Panel runat="server" ID="passengerdet">
</asp:Panel>
Server Side Code:
protected void AddP_Click(object sender, EventArgs e)
{
int rowCount = Convert.ToInt32(NoOfPsngr.Text);
Table table = new Table();
table.ID = "PsngrTbl";
//Create the textboxes and labels each time the button is clicked.
for (int i = 0; i < rowCount; i++)
{
TableRow row = new TableRow();
TableCell namelblCell = new TableCell();
Label namelbl = new Label();
namelbl.Text = "Name";
TableCell nameTxtCell = new TableCell();
TextBox nameTxt = new TextBox();
TableCell typelblCell = new TableCell();
Label typelbl = new Label();
typelbl.Text = "Type";
TableCell typeSelectCell = new TableCell();
DropDownList typeSelect = new DropDownList();
ListItem adultItem = new ListItem();
adultItem.Text = "Adult";
adultItem.Value = "Adult";
typeSelect.Items.Add(adultItem);
ListItem childItem = new ListItem();
childItem.Text = "Child";
childItem.Value = "Child";
typeSelect.Items.Add(childItem);
ListItem infantItem = new ListItem();
infantItem.Text = "Infant";
infantItem.Value = "Infant";
typeSelect.Items.Add(infantItem);
TableCell etktlblCell = new TableCell();
Label etktlbl = new Label();
etktlbl.Text = "Eticket No";
TableCell etktTxtCell = new TableCell();
TextBox etktTxt = new TextBox();
//Adding.....
namelblCell.Controls.Add(namelbl);
typelblCell.Controls.Add(typelbl);
etktlblCell.Controls.Add(etktlbl);
nameTxtCell.Controls.Add(nameTxt);
typeSelectCell.Controls.Add(typeSelect);
etktTxtCell.Controls.Add(etktTxt);
nameTxt.ID = "PName" + i;
typeSelect.ID = "PType" + i;
etktTxt.ID = "ETkt" + i;
row.Controls.Add(namelblCell);
row.Controls.Add(nameTxtCell);
row.Controls.Add(typelblCell);
row.Controls.Add(typeSelectCell);
row.Controls.Add(etktlblCell);
row.Controls.Add(etktTxtCell);
table.Rows.Add(row);
}
passengerdet.Controls.Add(table);
}

use javascript like
$('#PsngrTbl').find('input[type=radio]').each(function (index, element) {
var o = $(this);
var oID = o.attr("id");
var oValue;
var controlName = $(this).attr('name');
if ($('[name=' + controlName + ']:checked').val() == undefined) {
oValue = "";
}
else {
oValue = $('[name=' + controlName + ']:checked').val();
}
});
$('#PsngrTbl').find('input[type=checkbox]').each(function (index, element) {
var o = $(this);
var value;
if (o.on == true) {
value = 1;
}
else {
value = 0;
}
var oID = o.attr("id");
var oValue = value;
});
$('#PsngrTbl').find('textarea').each(function (index, element) {
var o = $(this);
var oID = o.attr("id");
var oValue = o.val();
});
$('#PsngrTbl').find('select').each(function (index, element) {
var o = $(this);
var oID = o.attr("id");
var oValue = o.val();
});
It will return all values of controlls of that table

Related

How can I restrict the width of cells in an HtmlTable in a Sharepoint 2010 WebPart?

I want to restrict the width of HTMLTableCells in a Sharepoint 2010 WebPart.
I can do it when I only have one row in the table like so:
var row1 = new HtmlTableRow();
var cellColTitle1 = new HtmlTableCell();
cellColTitle1.Width = "88px";
cellColTitle1.Style.Add("text-align", "center");
row1.Cells.Add(cellColTitle1);
Here's what I get, and it is fine:
...but when I have multiple rows it doesn't work - the cell widths are too wide, even though I have set them all to "88":
Here is the code in context (the code for rows 3 and 4 should be virtually identical to that for row 2, but am pasting it all for "full disclosure"):
private HtmlTable GetSection5Table()
{
HtmlTable dynamicTable = new HtmlTable();
dynamicTable.Border = 2;
// Create Row 1
var row1 = new HtmlTableRow();
var cellColTitle1 = new HtmlTableCell();
cellColTitle1.Width = "88px";
cellColTitle1.Style.Add("text-align", "center");
row1.Cells.Add(cellColTitle1);
var cellColTitle2 = new HtmlTableCell();
cellColTitle2.Width = "88px";
cellColTitle2.Style.Add("text-align", "center");
row1.Cells.Add(cellColTitle2);
var cellColTitle3 = new HtmlTableCell();
cellColTitle3.Width = "88px";
cellColTitle3.Style.Add("text-align", "center");
row1.Cells.Add(cellColTitle3);
var cellColTitle4 = new HtmlTableCell();
cellColTitle4.Width = "88px";
cellColTitle4.Style.Add("text-align", "center");
row1.Cells.Add(cellColTitle4);
var cellColTitle5 = new HtmlTableCell();
cellColTitle5.Width = "88px";
cellColTitle5.Style.Add("text-align", "center");
row1.Cells.Add(cellColTitle5);
var cellColTitle6 = new HtmlTableCell();
cellColTitle6.Width = "88px";
cellColTitle6.Style.Add("text-align", "center");
row1.Cells.Add(cellColTitle6);
var indexStr = new Label
{
CssClass = "finaff-webform-field-label",
Text = "Index"
};
cellColTitle1.Controls.Add(indexStr);
var fundStr = new Label
{
CssClass = "finaff-webform-field-label",
Text = "Fund"
};
fundStr.Style.Add("text-align", "center");
cellColTitle2.Controls.Add(fundStr);
var orgStr = new Label
{
CssClass = "finaff-webform-field-label",
Text = "Organization"
};
orgStr.Style.Add("text-align", "center");
cellColTitle3.Controls.Add(orgStr);
var accountStr = new Label
{
CssClass = "finaff-webform-field-label",
Text = "Account"
};
accountStr.Style.Add("text-align", "center");
cellColTitle4.Controls.Add(accountStr);
var activityStr = new Label
{
CssClass = "finaff-webform-field-label",
Text = "Activity"
};
activityStr.Style.Add("text-align", "center");
cellColTitle5.Controls.Add(activityStr);
var amountStr = new Label
{
CssClass = "finaff-webform-field-label",
Text = "Amount"
};
amountStr.Style.Add("text-align", "center");
cellColTitle6.Controls.Add(amountStr);
dynamicTable.Rows.Add(row1);
//// Create row 2
var row2 = new HtmlTableRow();
var cellColIndex1 = new HtmlTableCell();
cellColIndex1.Width = "88px";
row2.Cells.Add(cellColIndex1);
var cellColFund1 = new HtmlTableCell();
cellColFund1.Width = "88px";
row2.Cells.Add(cellColFund1);
var cellColOrg1 = new HtmlTableCell();
cellColOrg1.Width = "88px";
row2.Cells.Add(cellColOrg1);
var cellColAccount1 = new HtmlTableCell();
cellColAccount1.Width = "88px";
row2.Cells.Add(cellColAccount1);
var cellColActivity1 = new HtmlTableCell();
cellColActivity1.Width = "88px";
row2.Cells.Add(cellColActivity1);
var cellColAmount1 = new HtmlTableCell();
cellColAmount1.Width = "88px";
row2.Cells.Add(cellColAmount1);
boxIndex1 = new TextBox()
{
CssClass = "finaff-webform-field-input"//,
//Width = 88 //ADJUSTED_TEXTBOX_WIDTH
};
cellColIndex1.Controls.Add(boxIndex1);
boxFund1 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColFund1.Controls.Add(boxFund1);
boxOrganization1 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColOrg1.Controls.Add(boxOrganization1);
boxAccount1 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColAccount1.Controls.Add(boxAccount1);
boxActivity1 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColActivity1.Controls.Add(boxActivity1);
boxAmount1 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColAmount1.Controls.Add(boxAmount1);
dynamicTable.Rows.Add(row2);
// Row 3
var row3 = new HtmlTableRow();
var cellColIndex2 = new HtmlTableCell();
cellColIndex2.Width = "88px";
row3.Cells.Add(cellColIndex2);
var cellColFund2 = new HtmlTableCell();
cellColFund2.Width = "88px";
row3.Cells.Add(cellColFund2);
var cellColOrg2 = new HtmlTableCell();
cellColOrg2.Width = "88px";
row3.Cells.Add(cellColOrg2);
var cellColAccount2 = new HtmlTableCell();
cellColAccount2.Width = "88px";
row3.Cells.Add(cellColAccount2);
var cellColActivity2 = new HtmlTableCell();
cellColActivity2.Width = "88px";
row3.Cells.Add(cellColActivity2);
var cellColAmount2 = new HtmlTableCell();
cellColAmount2.Width = "88px";
row3.Cells.Add(cellColAmount2);
boxIndex2 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColIndex2.Controls.Add(boxIndex2);
boxFund2 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColFund2.Controls.Add(boxFund2);
boxOrganization2 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColOrg2.Controls.Add(boxOrganization2);
boxAccount2 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColAccount2.Controls.Add(boxAccount2);
boxActivity2 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColActivity2.Controls.Add(boxActivity2);
boxAmount2 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColAmount2.Controls.Add(boxAmount2);
dynamicTable.Rows.Add(row3);
// Row 4
var row4 = new HtmlTableRow();
var cellColIndex3 = new HtmlTableCell();
cellColIndex3.Width = "88px";
row4.Cells.Add(cellColIndex3);
var cellColFund3 = new HtmlTableCell();
cellColFund3.Width = "88px";
row4.Cells.Add(cellColFund3);
var cellColOrg3 = new HtmlTableCell();
cellColOrg3.Width = "88px";
row4.Cells.Add(cellColOrg3);
var cellColAccount3 = new HtmlTableCell();
cellColAccount3.Width = "88px";
row4.Cells.Add(cellColAccount3);
var cellColActivity3 = new HtmlTableCell();
cellColActivity3.Width = "88px";
row4.Cells.Add(cellColActivity3);
var cellColAmount3 = new HtmlTableCell();
cellColAmount3.Width = "88px";
row4.Cells.Add(cellColAmount3);
boxIndex3 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColIndex3.Controls.Add(boxIndex3);
boxFund3 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColFund3.Controls.Add(boxFund3);
boxOrganization3 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColOrg3.Controls.Add(boxOrganization3);
boxAccount3 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColAccount3.Controls.Add(boxAccount3);
boxActivity3 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColActivity3.Controls.Add(boxActivity3);
boxAmount3 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
cellColAmount3.Controls.Add(boxAmount3);
dynamicTable.Rows.Add(row4);
return dynamicTable;
}
Why does 1 row work, but multiple rows do not? The code is fundamentally the same for subsequent rows as it is for the initial row.
It turns out I have to specify the width of the TextBoxes, too. So these:
boxIndex1 = new TextBox()
{
CssClass = "finaff-webform-field-input"
};
...had to become like so:
boxIndex1 = new TextBox()
{
CssClass = "finaff-webform-field-input",
Width = 88
};
Now the whole shebang looks like it should.
UPDATE
Or, more glamorously (sorry, elegantly):
const int TEXTBOX_WIDTH = 88;
const String CELL_WIDTH = "88px";
. . .
var cellColTitle1 = new HtmlTableCell();
cellColTitle1.Width = CELL_WIDTH;
. . .
// Create row 2
. . .
boxIndex1 = new TextBox()
{
CssClass = "finaff-webform-field-input",
Width = TEXTBOX_WIDTH
};

Placing DataBound Drop Down List into TableCell

I have a bound drop down list that I would like to place into a Table Cell. I am used to Labels and Text Boxes but I cannot seem to get the syntax working on this one. This code is inside of a Webmethod and my output has to be just the html. Thank you.
[System.Web.Services.WebMethod]
public static string gatherSurchargeData(string PriceListItemID, string ProdID, string ColorCode)
{
DataTable GetProductSizes = new DataTable();
GetProductSizes = DataLayer.PricingToolDL.getProductSizes(ProdID, ColorCode);
DataTable dt = new DataTable();
dt = DataLayer.PricingToolDL.getScharge(PriceListItemID);
Table tblScharges = new Table();
tblScharges.ID = "tblScharges";
TableHeaderRow th = new TableHeaderRow();
TableHeaderCell thSizeScharge = new TableHeaderCell();
thSizeScharge.Text = "Size";
th.Cells.Add(thSizeScharge);
tblScharges.Rows.Add(th);
int i = 0;
while (i <= dt.Rows.Count - 1)
{
TableRow tr = new TableRow();
tr.ID = "tableTr" + i;
TableCell tcSizeScharge = new TableCell();
DropDownList ddl = new DropDownList();
ddl.DataSource = GetProductSizes;
ddl.DataTextField = "FitSize";
ddl.DataValueField = "FitSize";
ddl.DataBind();
//string dtMovexSKU = dt.Rows[i]["MovexSKU"].ToString();
//DataRow[] GetProductSizesMovexSKU = GetProductSizes.Select("MovexSKU Like'" + dtMovexSKU + "'");
//tcSizeScharge.ID = "tcSizeScharge" + i;
//tcSizeScharge.Text = GetProductSizesMovexSKU[0][1].ToString();
tr.Cells.Add(tcSizeScharge);
tblScharges.Rows.Add(tr);
i++;
}
string html = "";
using (StringWriter sw = new StringWriter())
{
tblScharges.RenderControl(new HtmlTextWriter(sw));
html = sw.ToString();
}
return html;
}
The commented lines would be the code I would use if I were only wanting text to appear if that helps.
You need to add control in tablecell like this
tcSizeScharge.Controls.Add(ddl);

Store list of hyperlinks

I have an aspx page that contains a grid-view. The data-source for the gird-view is a datatable.
I want the last column in the grid view to contain a list of hyperlinks. I cannot figure out how to store or display this data
private void retrieveGroups()
{
// UserTab is a DataTable
// UserGrid is a GridView
UserTab.Columns.Add("LoginName", typeof(string));
UserTab.Columns.Add("DisplayName", typeof(string));
UserTab.Columns.Add("Kind", typeof(string));
UserTab.Columns.Add("Count", typeof(string));
UserTab.Columns.Add("Managers", typeof(string));
UserGrid.DataSource = UserTab;
SqlCommand cmd = FmaDb.makeCmd(FmaDb.SubDB.FMA_CUSTOMER, "fetchGroupIds", CommandType.StoredProcedure,
delegate(SqlCommand inCmd) { inCmd.Parameters.Add("#siteid", SqlDbType.Int); });
List<int> groupIds = new List<int>();
FmaDb.runReadCmd(cmd, delegate(SqlCommand inCmd) { inCmd.Parameters["#siteid"].Value = int.Parse(LFList.SelectedItem.Value); }, null,
delegate(SqlDataReader qrdr) { groupIds.Add(FmaDb.getDbInt(qrdr, 0)); });
UserGrid.Visible = true;
HyperLinkField hf = new HyperLinkField();
BoundField bf = new BoundField();
hf.HeaderText = "Login";
hf.DataTextField = "LoginName";
hf.DataNavigateUrlFields = new String[1] { "login" };
hf.DataNavigateUrlFormatString = "~/fausage.aspx?cn=login&lg={0}&ad=" + ad + "&as=" + aStatus;
UserGrid.Columns.Add(hf);
bf = new BoundField();
bf.HeaderText = "Name";
bf.DataField = "DisplayName";
UserGrid.Columns.Add(bf);
bf = new BoundField();
bf.HeaderText = "Type";
bf.DataField = "Kind";
UserGrid.Columns.Add(bf);
bf = new BoundField();
bf.HeaderText = "# Members";
bf.DataField = "Count";
UserGrid.Columns.Add(bf);
bf = new BoundField();
bf.HeaderText = "Managers";
bf.DataField = "Managers";
UserGrid.Columns.Add(bf);
foreach (int gId in groupIds)
{
Advisor advGroup = new Advisor(gId);
List<HyperLink> linkList = new List<HyperLink>();
advGroup.fetch();
if ((advGroup.Kind == AdvisorKind.Branch) || (advGroup.Kind == AdvisorKind.Team))
{
List<AdvisorMembership> members = advGroup.MyMembers(null, false);
if (advGroup.Kind == AdvisorKind.Branch)
{
List<AdvisorMembership> mgrs = advGroup.MyMembers(null, true);
HyperLink link;
foreach (AdvisorMembership m in mgrs)
{
if (m.MemberCanLoginAsContainer)
{
link = new HyperLink();
link.Text = m.Member.LoginName;
link.NavigateUrl = "~/fausage.aspx?cn=login&lg=" + m.Member.LoginName + "&ad=" + ad + "&as=" + aStatus;
link.Target = "blank";
linkList.Add(link);
}
}
}
UserTab.Rows.Add(advGroup.LoginName, advGroup.DisplayName, advGroup.Kind.ToString(), members.Count.ToString(), linkList);
}
ViewState["DataTable"] = UserTab;
}
}
}
}

Grid view event not getting triggered

I have a gridview created dynamically , now i am trying to do edit each column by keeping a link button. But the rowdatabound event is not getting triggered.Where i might have gone wrong?
Below is my code :
dtValues = gObj.GetAllDocumentsHistoryList();
dtHeader = gObj.GetAllHeaderList();
GridView gvEmployee = new GridView();
gvEmployee.ShowHeaderWhenEmpty = true;
gvEmployee.EmptyDataText = "Sorry No History Records Found !!!!!!!";
gvEmployee.AutoGenerateColumns = false;
for (int i = 0; i < dtValues.Columns.Count; i++)
{
string name = dtValues.Columns[i].ColumnName.ToString();
BoundField boundfield = new BoundField();
boundfield.DataField = dtValues.Columns[i].ColumnName.ToString();
for (int j = 0; j < dtHeader.Rows.Count; j++)
{
if (dtHeader.Rows[j]["ColCode"].ToString() == dtValues.Columns[i].ColumnName.ToString())
{
boundfield.HeaderText = dtHeader.Rows[j]["ColName"].ToString();
if (boundfield.HeaderText.Contains("Date") || boundfield.HeaderText.Contains("DocExpiry"))
{
boundfield.DataFormatString = "{0:dd/MMM/yyyy}";
}
}
else if (dtValues.Columns[i].ColumnName.ToString() == "Last Modified Date")
{
boundfield.HeaderText = "Last Modified Date";
boundfield.DataFormatString = "{0:dd/MMM/yyyy}";
}
}
gvEmployee.Columns.Add(boundfield);
}
gvEmployee.DataSource = dtValues;
gvEmployee.DataBind();
gvEmployee.Width = new Unit("90%");
gvEmployee.RowDataBound += new GridViewRowEventHandler(gvEmployee_RowDataBound);
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton lnkView = new LinkButton();
lnkView.ID = "lnkView";
lnkView.Text = "View";
lnkView.Click += ViewDetails;
lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row["Id"].ToString();
e.Row.Cells[3].Controls.Add(lnkView);
}
Just add your RowDataBound handler before DataBind.
Corrected Code:
gvEmployee.DataSource = dtValues;
gvEmployee.RowDataBound += new GridViewRowEventHandler(gvEmployee_RowDataBound);
gvEmployee.DataBind();
gvEmployee.Width = new Unit("90%");

Get Data from a table which has been generated programmatically

I am creating a Control Table programmatically and I want after user clicks a button to get the values.
The table has three columns, {Title(DropDownList), FName(TextBox), LName(TextBox)} and two rows.
I am getting the values for FName, LName without problem, but I am getting wrong value for Title, which is a DropDownList. It gives me for both Titles, the selected value of the second column.
More Clear,
For the 1st row --> Mr, Foo, Bar
for 2nd row- -> Mrs, Foo, Bar
after user clicks the button I am getting
For 1st row --> Mrs, Foo, Bar
For 2nd row- -> Mrs, Foo1, Bar.
This is the method of table creation
private void SetPassengerDetailsTable(int roomIdentity, int? adults,
int? children, int? infants, bool setLeader = false) {
var leaderIsSet = false;
var roomTable = new Table();
roomTable.ID = "PassengerDetailsTBL_" + roomIdentity;
var trHeader = new TableHeaderRow();
var tcTitle = new TableCell();
tcTitle.Controls.Add(new Label() { Text = "<b>Title</b>" });
trHeader.Cells.Add(tcTitle);
var tcFName = new TableCell();
tcFName.Controls.Add(new Label() { Text = "<b>First Name</b>" });
trHeader.Cells.Add(tcFName);
var tcLName = new TableCell();
tcLName.Controls.Add(new Label() { Text = "<b>Last Name</b>" });
trHeader.Cells.Add(tcLName);
var tcType = new TableCell();
tcType.Controls.Add(new Label() { Text = "<bType</b>" });
trHeader.Cells.Add(tcType);
roomTable.Rows.Add(trHeader);
var listItems = new ListItem[6];
listItems[0] = new ListItem("", "") { Selected = true };
listItems[1] = new ListItem("Mr", "Mr");
listItems[2] = new ListItem("Mrs", "Mrs");
listItems[3] = new ListItem("Miss", "Miss");
listItems[4] = new ListItem("Infant", "Inf");
listItems[5] = new ListItem("Child", "Master");
if (adults != null || adults > 0) {
for (int i = 1; i <= adults; i++) {
var trBody = new TableRow();
var ddl = new DropDownList();
var leadAdultRowID = setLeader && !leaderIsSet ? "leadadult" : "adult";
ddl.ID = "room" + roomIdentity + leadAdultRowID + i + "Title";
ddl.Items.AddRange(listItems);
var tcTitleValue = new TableCell();
tcTitleValue.Controls.Add(ddl);
trBody.Cells.Add(tcTitleValue);
var tcFNameValue = new TableCell();
tcFNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "adult" + i + "FName", Width = 170 });
trBody.Cells.Add(tcFNameValue);
var tcLNameValue = new TableCell();
tcLNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "adult" + i + "LName", Width = 170 });
trBody.Cells.Add(tcLNameValue);
var tcTypeValue = new TableCell();
var leadAdult = setLeader && !leaderIsSet ? "Lead Adult" : "Adult";
tcTypeValue.Controls.Add(new Label() { Text = leadAdult });
trBody.Cells.Add(tcTypeValue);
roomTable.Rows.Add(trBody);
if (setLeader)
leaderIsSet = true;
}
}
if (children != null || children > 0) {
for (int i = 1; i <= children; i++) {
var trBody = new TableRow();
var ddl = new DropDownList();
ddl.ID = "room" + roomIdentity + "child" + i + "Title";
ddl.Items.AddRange(listItems);
var tcTitleValue = new TableCell();
tcTitleValue.Controls.Add(ddl);
trBody.Cells.Add(tcTitleValue);
var tcFNameValue = new TableCell();
tcFNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "child" + i + "FName", Width = 170 });
trBody.Cells.Add(tcFNameValue);
var tcLNameValue = new TableCell();
tcLNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "child" + i + "LName", Width = 170 });
trBody.Cells.Add(tcLNameValue);
var tcTypeValue = new TableCell();
tcTypeValue.Controls.Add(new Label() { Text = "Child" });
trBody.Cells.Add(tcTypeValue);
roomTable.Rows.Add(trBody);
}
}
if (infants != null || infants > 0) {
for (int i = 1; i <= infants; i++) {
var trBody = new TableRow();
var ddl = new DropDownList();
ddl.ID = "room" + roomIdentity + "infan" + i + "Title";
ddl.Items.AddRange(listItems);
var tcTitleValue = new TableCell();
tcTitleValue.Controls.Add(ddl);
trBody.Cells.Add(tcTitleValue);
var tcFNameValue = new TableCell();
tcFNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "infan" + i + "FName", Width = 170 });
trBody.Cells.Add(tcFNameValue);
var tcLNameValue = new TableCell();
tcLNameValue.Controls.Add(new TextBox() { ID = "room" + roomIdentity + "infan" + i + "LName", Width = 170 });
trBody.Cells.Add(tcLNameValue);
var tcTypeValue = new TableCell();
tcTypeValue.Controls.Add(new Label() { Text = "Infant" });
trBody.Cells.Add(tcTypeValue);
roomTable.Rows.Add(trBody);
}
}
PassengerDetailsPH.Controls.Add(roomTable);
}
Thanks
Double check what client-side IDs are generated for your drop downs.
If you table is rendered correctly (with correct IDs which must be unique), then the code which reads is reading from the wrong element.
create a new listItems list for every instance of the DropDownList
var ddlItems = listItems.ToArray();
ddl.Items.Addrange(ddlItems);
(have you looked at the GridView control?)

Resources