Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I'll have to display one lable in one column and a dropdown in the other column. Upon the selection of value from the first dropdown list we will have to display the second question with options. like that we will have to dynamically generate the rows.
please help me out on this
The code I have tried is:
 
C# code:
private int numOfRows = 0;
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GenerateTable(numOfRows);
            }
        }
 
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value1 = DropDownList1.SelectedItem.ToString();
            if (value1 == "USCS")
            {
                //DropDownList1.Enabled = false;
                if (ViewState["RowsCount"] != null)
                {
                    numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
                    GenerateTable(numOfRows);
                }
            }
        }
 
        private void SetPreviousData(int rowsCount, int colsCount)
        {
            if (this.Table1== null)
            {
                for (int i = 0; i <= rowsCount; i++)
                {
                    for (int j = 0; j <= colsCount; j++)
                    {
                        //Extracting the Dynamic Controls from the Table
                        Label lbl = (Label)Table1.Rows[i].Cells[j].FindControl("Label_" + i + "Col_" + j);
                      //  DropDownList ddl  = (DropDownList)Table1.Rows[i].Cells[j].FindControl("Label_" + i + "Col_" + j);
                        //Use Request objects for getting the previous data of the dynamic textbox
                        lbl.Text = Request.Form["Label_" + i + "Col_" + j];
                     //   ddl.Text = Request.Form["DropDownList_" + i + "Col_" + j];
                    }
                }
            }
        }
 
        private void GenerateTable(int rowsCount)
        {
            const int colsCount = 2;
            for (int i = 1; i < rowsCount; i++)
            {
                TableRow row = new TableRow();
                for (int j = 0; j < colsCount; j++)
                {
 
                    if (colsCount.Equals(1))
                    {
                        TableCell cell = new TableCell();
                        Label lbl = new Label();
                        lbl.ID =  "Label" + j;
                        cell.Controls.Add(lbl);
                        row.Cells.Add(cell);
                    }
 
                    else if (colsCount.Equals(2))
                    {
                        TableCell cell1 = new TableCell();
                        DropDownList ddl = new DropDownList();
                        ddl.ID = "DropDownList" + i;
                        cell1.Controls.Add(ddl);
                        row.Cells.Add(cell1);
                    }
 

              
                    // Set a unique ID for each TextBox added
                    
                    // Add the TableCell to the TableRow

                }
 
                // And finally, add the TableRow to the Table
                this.Table1.Rows.Add(row);
            }
 
            //Set Previous Data on PostBacks
            SetPreviousData(rowsCount, colsCount);
 
            //Sore the current Rows Count in ViewState
            rowsCount++;
            ViewState["RowsCount"] = rowsCount;
        }
 

ASP code:
 
<body>
    <form id="form1" runat="server">
    <div>
        <%--<table style="width: 100%;" >--%>
        <asp:Table ID="Table1" runat="server">
             <asp:TableRow>
               <asp:TableCell>
 
                <asp:Label ID="Label1" runat="server" Text="Questions"></asp:Label>
 
                    &nbsp;
               </asp:TableCell>
               <asp:TableCell>
 
                <asp:Label ID="Label2" runat="server" Text="Answers"></asp:Label>
 
                    &nbsp;
                </asp:TableCell>
 
             </asp:TableRow>
             <asp:TableRow>
                  <asp:TableCell>
 
                <asp:Label ID="Label3" runat="server" Text="Base Question?"></asp:Label>
 
                    &nbsp;
               </asp:TableCell>
                <asp:TableCell>
 
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
 
                    <asp:ListItem Selected="True" Text="--Select--" Value="--Select--"></asp:ListItem>
                <asp:ListItem Text="USCS" Value="USCS"></asp:ListItem>
                <asp:ListItem Text="NONUSCS" Value="NONUSCS"></asp:ListItem>
 
                           </asp:DropDownList>
 
                    &nbsp;
 
                </asp:TableCell>
 
             </asp:TableRow>
            </asp:Table>
            <%--</table>--%>
            <%--<asp:Table ID="Table1" runat="server"></asp:Table>--%>
 
    </form>
 
</body>
Posted 24-Nov-12 20:32pm
arevant257
Edited 24-Nov-12 21:24pm
(no name)177.7K
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 Raul Iloc 170
4 Maciej Los 164
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 25 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100