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;
 
<pre>
    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 &lt;= rowsCount; i++)
            {
                for (int j = 0; j &lt;= 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 &lt; rowsCount; i++)
        {
            TableRow row = new TableRow();
            for (int j = 0; j &lt; 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;
    }</pre>

ASP code:
 
<body>
    <form id="form1" runat="server">
    <div>
        <%--<table style="width: 100%;" >--%>
        <asp:Table ID="Table1" runat="server">
             <asp:TableRow>
               <asp:TableCell>
 
<pre>
            &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&quot;Questions&quot;&gt;&lt;/asp:Label&gt;
 
                &amp;nbsp;
           &lt;/asp:TableCell&gt;
           &lt;asp:TableCell&gt;
 
            &lt;asp:Label ID=&quot;Label2&quot; runat=&quot;server&quot; Text=&quot;Answers&quot;&gt;&lt;/asp:Label&gt;
 
                &amp;nbsp;
            &lt;/asp:TableCell&gt;
 
         &lt;/asp:TableRow&gt;
         &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;
 
            &lt;asp:Label ID=&quot;Label3&quot; runat=&quot;server&quot; Text=&quot;Base Question?&quot;&gt;&lt;/asp:Label&gt;
 
                &amp;nbsp;
           &lt;/asp:TableCell&gt;
            &lt;asp:TableCell&gt;
 
            &lt;asp:DropDownList ID=&quot;DropDownList1&quot; runat=&quot;server&quot; AutoPostBack=&quot;True&quot;
                    onselectedindexchanged=&quot;DropDownList1_SelectedIndexChanged&quot;&gt;
 
                &lt;asp:ListItem Selected=&quot;True&quot; Text=&quot;--Select--&quot; Value=&quot;--Select--&quot;&gt;&lt;/asp:ListItem&gt;
            &lt;asp:ListItem Text=&quot;USCS&quot; Value=&quot;USCS&quot;&gt;&lt;/asp:ListItem&gt;
            &lt;asp:ListItem Text=&quot;NONUSCS&quot; Value=&quot;NONUSCS&quot;&gt;&lt;/asp:ListItem&gt;
 
                       &lt;/asp:DropDownList&gt;
 
                &amp;nbsp;
 
            &lt;/asp:TableCell&gt;
 
         &lt;/asp:TableRow&gt;
        &lt;/asp:Table&gt;
        &lt;%--&lt;/table&gt;--%&gt;
        &lt;%--&lt;asp:Table ID=&quot;Table1&quot; runat=&quot;server&quot;&gt;&lt;/asp:Table&gt;--%&gt;
 
&lt;/form&gt;

</body>
Posted 24-Nov-12 20:32pm
arevant261
Edited 24-Nov-12 21:24pm
(no name)181.3K
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 480
1 Sergey Alexandrovich Kryukov 445
2 Maciej Los 290
3 ProgramFOX 275
4 CHill60 225
0 OriginalGriff 550
1 Sergey Alexandrovich Kryukov 470
2 Maciej Los 310
3 ProgramFOX 275
4 Peter Leow 250


Advertise | Privacy | Mobile
Web01 | 2.8.150331.1 | Last Updated 25 Nov 2012
Copyright © CodeProject, 1999-2015
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