我正在以编程方式创建一个控制表,我希望在用户单击按钮后获取值。
该表有三列,{Title( DropDownList ),FName( TextBox ),LName( TextBox )}和两行。
我正在获取FName,LName的值没有问题,但是我得到了Title的错误值,这是一个DropDownList。它为我提供了两个标题,即第二列的选定值。
更清楚,
第一行 - >先生,Foo,Bar
第二行- >夫人,Foo,Bar
用户点击我的按钮后
第一行 - > Mrs ,Foo,Bar
第二行 - >夫人,Foo1,Bar。
这是创建表的方法
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);
}
由于
答案 0 :(得分:1)
仔细检查为您的下拉列表生成哪些客户端ID。
如果表格被正确呈现(正确的ID必须是唯一的),那么读取的代码就是从错误的元素中读取。
答案 1 :(得分:0)
为DropDownList
的每个实例创建一个新的listItems列表var ddlItems = listItems.ToArray();
ddl.Items.Addrange(ddlItems);
(你看过GridView控件吗?)