Sorting table when used with literalControl ASP.net - asp.net

I have a problem sorting this table. This was just a project that was pass unto me, and they want me to make some changes with the codes. Here's the code used to create table with data.
DataTable dt = common.get_entry(sname);
if (dt.Rows.Count != 0)
{
error_msg.Visible = false;
loader4.Visible = true;
}
else
{
loader4.Visible = false;
error_msg.Visible = true;
}
int maxsize = 10;
int rpage = Convert.ToInt32(p);
int startrow = ((rpage - 1) * maxsize) + 1;
int endrow = (rpage * maxsize);
if (dt.Rows.Count > 0)
{
int rcnt = dt.Rows.Count;
var tbl = view_tbl;
view_tbl.CssClass = "table table-hover table-bordered";
var maxpage = rcnt / maxsize;
var excess = rcnt % maxsize;
if (excess > 0) maxpage++;
TableHeaderRow r1 = new TableHeaderRow();
r1.TableSection = TableRowSection.TableHeader;
TableHeaderCell c2 = new TableHeaderCell();
c2.Controls.Add(new LiteralControl(""));
c2.Width = 45;
r1.Cells.Add(c2);
TableHeaderCell c8 = new TableHeaderCell();
c8.Controls.Add(new LiteralControl("Store Code"));
c8.Width = 120;
r1.Cells.Add(c8);
TableHeaderCell c3 = new TableHeaderCell();
c3.Controls.Add(new LiteralControl("Store Name"));
c3.Width = 250;
r1.Cells.Add(c3);
TableHeaderCell c4 = new TableHeaderCell();
c4.Controls.Add(new LiteralControl("Line of Business"));
c4.Width = 150;
r1.Cells.Add(c4);
TableHeaderCell c5 = new TableHeaderCell();
c5.Controls.Add(new LiteralControl("Visitors Name"));
c5.Width = 170;
r1.Cells.Add(c5);
TableHeaderCell c7 = new TableHeaderCell();
c7.Controls.Add(new LiteralControl("Date of Visit"));
c7.Width = 100;
r1.Cells.Add(c7);
TableHeaderCell c9 = new TableHeaderCell();
c9.Controls.Add(new LiteralControl("Added Date"));
c9.Width = 170;
r1.Cells.Add(c9);
TableHeaderCell c6 = new TableHeaderCell();
c6.Controls.Add(new LiteralControl("Action"));
c6.Width = 100;
r1.Cells.Add(c6);
tbl.Rows.Add(r1);
for (int cnt = startrow - 1; cnt < rcnt && cnt < endrow; cnt++)
{
TableRow r = new TableRow();
String sn = dt.Rows[cnt][3].ToString();
String lo = dt.Rows[cnt][5].ToString();
String id = dt.Rows[cnt][0].ToString();
String vn = dt.Rows[cnt][4].ToString();
String ed = dt.Rows[cnt][1].ToString();
String da = dt.Rows[cnt][6].ToString();
String sc = dt.Rows[cnt][7].ToString();
string[] edd = new string[2];
edd = ed.Split(' ');
ed = edd[0] + " " + dt.Rows[cnt][2].ToString();
TableCell cc1 = new TableCell();
cc1.Controls.Add(new LiteralControl("<input class='check_box' rel='" + id + "'type='checkbox'> <a href='#'><i class='icon-star-empty'></i></a>"));
r.Cells.Add(cc1);
TableCell cc2 = new TableCell();
cc2.Controls.Add(new LiteralControl("<strong>" + sc + "</strong>"));
r.Cells.Add(cc2);
TableCell cc7 = new TableCell();
cc7.Controls.Add(new LiteralControl("<strong>" + sn + "</strong>"));
r.Cells.Add(cc7);
TableCell cc3 = new TableCell();
cc3.Controls.Add(new LiteralControl("<strong>" + lo + "</strong>"));
r.Cells.Add(cc3);
TableCell cc4 = new TableCell();
cc4.Controls.Add(new LiteralControl("<strong>" + vn + "</strong>"));
r.Cells.Add(cc4);
TableCell cc5 = new TableCell();
cc5.Controls.Add(new LiteralControl("<strong>" + ed + "</strong>"));
r.Cells.Add(cc5);
TableCell cc8 = new TableCell();
cc8.Controls.Add(new LiteralControl("<strong>" + da + "</strong>"));
r.Cells.Add(cc8);
TableCell cc6 = new TableCell();
cc6.Controls.Add(new LiteralControl("<a data-target='#viewentry' data-toggle='modal' class='btn btn-mini btn-success view_btn' rel='" + id + "'><i class='icon-th-list icon-white'></i> View</a> <a data-target='#delentry' data-toggle='modal' class='btn btn-mini btn-danger del_btn hidden' rel='" + id + "'><i class='icon-trash icon-white'></i></a>"));
r.Cells.Add(cc6);
tbl.Rows.Add(r);
}
var dpagi = pagi;
LiteralControl ul = new LiteralControl();
ul.Text = "<ul>";
for (int cnt5 = 1; cnt5 <= maxpage; cnt5++)
{
if (cnt5 == rpage)
ul.Text = ul.Text + "<li class='disabled'><a href='StoreVisitView.aspx?page=" + cnt5 + "'>" + cnt5 + "</a></li>";
else
ul.Text = ul.Text + "<li><a href='StoreVisitView.aspx?page=" + cnt5 + "' class='pgn'>" + cnt5 + "</a></li>";
}
ul.Text = ul.Text + "</ul>";
dpagi.Controls.Add(ul);
pagi.CssClass = "pagination";
loader4.Visible = false;
I have no idea how to sort this because the table was made thru literalcontrol and etc..

If you want to sort the data on server side I suggest using a DataView to sort it. After it's sorted you can render it manually as you do now (iterate through DataView's rows) or bind a DataGrid to it.
DataView view = new DataView(dt);
view.Sort = "Column ASC, Column2 ASC";
// Now iterate through rows of dataview
foreach (DataRowView row in view) {...}
Other option is to sort it on client side using some sorting plugin.
For example Tablesorter seems pretty easy to use. Or just google "sort table javascript"...

Related

Unable to update the textbox values in the gridview[c# asp.net]

Getting old values in DataTable, I need new values which I updated. Please help me?
Update on button click
My code:
DataSet Ds = new DataSet();
string Query = "";
Query = "select Conv_0,Unit_0,Rate_0,Text_0,Barcode_0,Conv_1,Unit_1,Rate_1,Text_1,Barcode_1,Conv_2,Unit_2,Rate_2,Text_2,Barcode_2,Conv_3,Unit_3,Rate_3,Text_3,Barcode_3,Conv_4,Unit_4,Rate_4,Text_4,Barcode_4 from Prod_Unit where Code='" + a + "'";
Ds = SqlClass1.GetData_from_localhost(Query);
if (Ds.Tables[0].Rows.Count > 0)
{
dt.Columns.Clear();
dt.Rows.Clear();
dt.Columns.Add("NO");
dt.Columns.Add("UNITS");
dt.Columns.Add("CONVERSION");
dt.Columns.Add("BARCODE");
dt.Columns.Add("RATE 1");
dt.Columns.Add("RATE 2");
dt.Columns.Add("PRINT TEXT");
// dt.Rows.Add();
for (int i = 0; i < Ds.Tables[0].Columns.Count; i++)
{
if (i >= 0 && i <= 4)
{
dt.Rows.Add();
dt.Rows[i]["NO"] = i + 1;
string unit = Ds.Tables[0].Rows[0]["Unit_" + i + ""].ToString();
DataSet temp = SqlClass1.GetData_from_localhost("select Unit FROM Units where Code=" + unit + "");
if (temp.Tables[0].Rows.Count > 0)
{
dt.Rows[i]["UNITS"] = temp.Tables[0].Rows[0][0].ToString();
}
dt.Rows[i]["CONVERSION"] = Ds.Tables[0].Rows[0]["Conv_" + i + ""].ToString();
dt.Rows[i]["BARCODE"] = Ds.Tables[0].Rows[0]["Barcode_" + i + ""].ToString();
dt.Rows[i]["RATE 1"] = Ds.Tables[0].Rows[0]["Rate_" + i + ""].ToString();
dt.Rows[i]["RATE 2"] = Ds.Tables[0].Rows[0]["Rate_" + i + ""].ToString();
dt.Rows[i]["PRINT TEXT"] = Ds.Tables[0].Rows[0]["Text_" + i + ""].ToString();
}
}
if (i == 0)
{
// ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
// SetPreviousData();
}
}
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
SetPreviousData();// function to get previous data in
textbox
This is my binding function that I call in postback. How can I get updated values in ViewState["CurrentTable"] = dt;
on Update Button
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("NO");
dt.Columns.Add("UNITS");
dt.Columns.Add("CONVERSION");
dt.Columns.Add("BARCODE");
dt.Columns.Add("RATE 1");
dt.Columns.Add("RATE 2");
dt.Columns.Add("PRINT TEXT");
dr = dt.NewRow();
dr["NO"] = 1;
dr["UNITS"] = string.Empty;
dr["CONVERSION"] = string.Empty;
dr["BARCODE"] = string.Empty;
dr["RATE 1"] = string.Empty;
dr["RATE 2"] = string.Empty;
dr["PRINT TEXT"] = string.Empty;
dt.Rows.Add(dr);
int rowIndex = 0;
for (int i = 0; i < GridView1.Rows.Count; i ++)
{
if (GridView1.Rows[rowIndex].RowType == DataControlRowType.DataRow)
{
DropDownList box1 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
TextBox box5 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
TextBox box6 = (TextBox)GridView1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
dt.Rows[i]["NO"] = i + 1;
dt.Rows[i]["Units"] = box1.Text;
dt.Rows[i]["Conversion"] = box2.Text;
dt.Rows[i]["Barcode"] = box3.Text;
dt.Rows[i]["Rate 1"] = box4.Text;
dt.Rows[i]["Rate 2"] = box5.Text;
dt.Rows[i]["Print Text"] = box6.Text;
dt.Rows.Add();
rowIndex++;
}
}
ViewState["CurrentTable"] = dt;
Save();//function for save
}

how to add button in Dynamic gridview?

public void BuildMeatTally()
{
try
{
DataTable dtdate = new DataTable();
objRetailPL.status = 4;
dtdate = objRetailBAL.GetTypeNew(objRetailPL);
if (dtdate.Rows.Count > 0)
{
for (int i = 0; i < dtdate.Rows.Count; i++)
{
string branchdate;
int totbirds; float totwt, meatyield, Avgweight;
DateTime datewr;
DataTable dtdatedetailsTable = new DataTable();
objRetailPL.branchdate = dtdate.Rows[i]["BranchDate"].ToString();
objRetailPL.sno = Convert.ToInt32(dtdate.Rows[i]["sno"].ToString());
dtdatedetailsTable = objRetailBAL.getbradisdatewisedet(objRetailPL);
branchdate = dtdatedetailsTable.Rows[0]["BranchDate"].ToString();
totbirds = Convert.ToInt32(dtdatedetailsTable.Rows[0]["numofbirds"].ToString());
totwt = Convert.ToSingle(dtdatedetailsTable.Rows[0]["totalweght"].ToString());
meatyield = Convert.ToSingle(dtdatedetailsTable.Rows[0]["totalmeatyeild"].ToString());
Avgweight = Convert.ToSingle(dtdatedetailsTable.Rows[0]["AvgWeight"].ToString());
Table tbldynamic = new Table();
tbldynamic.BorderStyle = BorderStyle.Ridge;
tbldynamic.Width = 600;
TableCell tc = new TableCell();
TableRow tr = new TableRow();
Label lblbranchdate = new Label();
lblbranchdate.ID = "lblbrandate" + i.ToString();
lblbranchdate.Text = "&nbsp&nbsp" + "<B>Branch Date:</B>" + "&nbsp";
tc.Controls.Add(lblbranchdate);
Label lblbranchdatedata = new Label();
lblbranchdatedata.ID = "lbltotnumbird1" + i.ToString();
lblbranchdatedata.Text = branchdate + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lblbranchdatedata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lbltotwt1 = new Label();
lbltotwt1.ID = "lbltotwt1" + i.ToString();
lbltotwt1.Text = "<B>Total Weight:</B>" + "&nbsp";
tc.Controls.Add(lbltotwt1);
Label lbltotwt1data = new Label();
lbltotwt1data.ID = "lbltotwt1data" + i.ToString();
lbltotwt1data.Text = totwt + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lbltotwt1data);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
tc = new TableCell();
tr = new TableRow();
Label lbltotbirds = new Label();
lbltotbirds.ID = "lbltotbird" + i.ToString();
lbltotbirds.Text = "<B>Total Birds:</B>" + "&nbsp";
tc.Controls.Add(lbltotbirds);
Label lbltotbirddata = new Label();
lbltotbirddata.ID = "lbltotbirdata" + i.ToString();
lbltotbirddata.Text = totbirds + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lbltotbirddata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lblmeatyield = new Label();
lblmeatyield.ID = "lblmeatyield" + i.ToString();
lblmeatyield.Text = "<B>Meat Yield in Kgs:</B>" + "&nbsp";
tc.Controls.Add(lblmeatyield);
Label lblmeatyielddata = new Label();
lblmeatyielddata.ID = "lblmeatyied" + i.ToString();
lblmeatyielddata.Text = meatyield + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lblmeatyielddata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lblavgweight = new Label();
lblavgweight.ID = "lblavgweight" + i.ToString();
lblavgweight.Text = "<B>Avg Weight:</B>" + "&nbsp";
tc.Controls.Add(lblavgweight);
Label lblavgweightdata = new Label();
lblavgweightdata.ID = "lblavgweightdata" + i.ToString();
lblavgweightdata.Text = Avgweight + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lblavgweightdata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
DataTable dtdcDetails = new DataTable();
objRetailPL.sno = Convert.ToInt32(dtdate.Rows[i]["sno"].ToString());
dtdcDetails = objRetailBAL.dtdateTallyTable(objRetailPL);
GridView gv = new GridView();
//gv.ID = "gv" + dtdate.Rows[i]["BranchDate"].ToString();
gv.Width = 600;
Label lblTotal = new Label();
Label lbltotaleggs = new Label();
BoundField partyname = new BoundField();
partyname.HeaderText = "PartyName";
partyname.DataField = "partyname";
gv.Columns.Add(partyname);
BoundField birdtype = new BoundField();
birdtype.HeaderText = "Bird Type";
birdtype.DataField = "birdname";
gv.Columns.Add(birdtype);
BoundField totweight = new BoundField();
totweight.HeaderText = "Total Weight";
totweight.DataField = "totalweight";
gv.Columns.Add(totweight);
BoundField rateperkg = new BoundField();
rateperkg.HeaderText = "Rate/Kg";
rateperkg.DataField = "rateperkg";
gv.Columns.Add(rateperkg);
BoundField Bdcno = new BoundField();
Bdcno.HeaderText = "DCNo";
Bdcno.DataField = "dcno";
gv.Columns.Add(Bdcno);
// Here I want to add Button(confirm) In dynamic Gridview..
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
gv.DataSource = dtdcDetails;
gv.DataBind();
}
}
}
how can i add button control in Dynamic gridview , i tried with Templeate field, but i am not getting exactly output...after how can i add Button Click event for it? please help me?
Thanks in advance...
Sambasiva
You cannot add control dynamically in GridView Control,
You need to create a class and extends it ITemplate.
Following links will explain it
http://www.codedigest.com/Articles/ASPNET/168_Create_Dynamic_GridView_Control_in_C_ASPNet.aspx
http://www.codeproject.com/Articles/13462/How-to-create-template-columns-dynamically-in-a-gr

how to retrieve sheet names into dropdownlist

DataTable dtExcelSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
ArrayList aList = new ArrayList();
foreach (DataRow row in dtExcelSheetName.Rows)
{
aList.Add(row);
}
drpSeletSheet.DataSource = aList;
for (int i = 1; i < dtExcelRecords.Rows.Count + 1; i++)
{
drpSeletSheet.Items.Add(dtExcelRecords.Rows[i]["Table_Name"].ToString());
}
drpSeletSheet.DataBind();
objAdapter1.SelectCommand = cmd;
objAdapter1.Fill(dtExcelRecords);

Object reference not set to an instance of an object-exception

I have a panel with ID:Panel2 in my web page and I'm adding adynamic table into it.
I'm getting an "Object reference not set to an instance of an object" exception.
My code:
for (int i = 0; i <= val; i++)
{
Table Table4 = new Table();
TableHeaderRow thr = new TableHeaderRow();
TableHeaderCell thc2 = new TableHeaderCell();
thc2.BorderColor = Color.Black;
thc2.BorderWidth = 2;
TableHeaderCell thc3 = new TableHeaderCell();
thc3.BorderColor = Color.Black;
thc3.BorderWidth = 2;
TableHeaderCell thc4 = new TableHeaderCell();
thc4.BorderColor = Color.Black;
thc4.BorderWidth = 2;
TableHeaderCell thc5 = new TableHeaderCell();
thc5.BorderColor = Color.Black;
thc5.BorderWidth = 2;
Label l2 = new Label();
l2.Text = "Check Point";
l2.ForeColor = Color.Black;
Label l3 = new Label();
l3.Text = "Applicability";
l3.ForeColor = Color.Black;
Label l4 = new Label();
l4.Text = "Effectiveness";
l4.ForeColor = Color.Black;
Label l5 = new Label();
l5.Text = "Score";
l5.ForeColor = Color.Black;
thc2.Controls.Add(l2);
thc3.Controls.Add(l3);
thc4.Controls.Add(l4);
thc5.Controls.Add(l5);
thr.Cells.Add(thc2);
thr.Cells.Add(thc3);
thr.Cells.Add(thc4);
thr.Cells.Add(thc5);
Table4.Rows.Add(thr);
TableRow tr = new TableRow();
tr.BorderColor = Color.Black;
tr.BorderWidth = 2;
TableCell c2 = new TableCell();
c2.BorderColor = Color.Black;
c2.BorderWidth = 2;
c2.Text = Convert.ToString(s[i]);
TableCell c3 = new TableCell();
c3.BorderColor = Color.Black;
c3.BorderWidth = 2;
TableCell c4 = new TableCell();
c4.BorderColor = Color.Black;
c4.BorderWidth = 2;
TableCell c5 = new TableCell();
c5.BorderColor = Color.Black;
c5.BorderWidth = 2;
DropDownList ddl1 = new DropDownList();
ddl1.Items.AddRange(items2);
ddl1.ID = "ddl1" + i;
DropDownList ddl2 = new DropDownList();
ddl2.Items.AddRange(items1);
ddl2.ID = "ddl2" + i;
TextBox t4=new TextBox();
t4.ID = "textID4" + i;
t4.EnableViewState = true;
c2.ID = "newC2" + i;
c3.ID = "newC3" + i;
c4.ID = "newC4" + i;
c5.ID = "newC5" + i;
tr.ID = "newRow" + i;
c3.Controls.Add(ddl1);
c4.Controls.Add(ddl2);
c5.Controls.Add(t4);
tr.Cells.Add(c2);
tr.Cells.Add(c3);
tr.Cells.Add(c4);
tr.Cells.Add(c5);
Table4.Rows.Add(tr);
-----> this.Panel2.Controls.Add(Table4);
Session["table"] = Table4;
}
In line (this.Panel2.Controls.Add(Table4)), I'm getting
Object reference not set to an instance of an object-exception.
even though I have created a Panel in the design page I'm getting the error
Here any table is added dynamically not the Panel, Panel is created statically and I'm inserting dynamic table in it.
There are a couple of reasons this can happen, but the most important is:
Are you absolutely sure you have created a control with the exact id
with the runat attribute set to "server"?
This applies to both Panel2 and Table4.
Other than that you need to check these things:
Your class (where the exception is occuring) is inheriting from System.UI.Page
The #Page CodeBehind attribute is set to your class (spelling, capitalisation)
You're referencing the object at the correct time during the page lifecycle (i.e. before it's being initialised)
There's no where that the reference has been modified prior to use

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