开发者

Get Data from a table which has been generated programmatically

开发者 https://www.devze.com 2023-02-06 14:00 出处:网络
I am creating a Control Table programmatically and I want after user clicks a button to get the values.

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?)

0

精彩评论

暂无评论...
验证码 换一张
取 消