locked
Find dynamically added controls to Panel in ASP.NET using C# RRS feed

  • Question

  • Hello All,

    I am using Visual Studio 2010, and developing a web project. My requirement is to add controls to web page dynamically. I have added the controls and those are displaying properly on the web page.

    But when I am trying to read values from the controls, I am unable to find the controls.

    Below is the sample code.

    int i;
    TableCell tcSNum;
    i = 0;
    foreach (DataRow drStudent in dsStudent.Tables[0].Rows)
    {
    tr = new TableRow();
    tr.BorderColor = System.Drawing.Color.Blue;
    tcSNum = new TableCell();
    tcSNum.ID = "TCNUM" + i.ToString();
    lblSName = new Label();
    lblSName.ID = "LBLSNAME" + i.ToString();
    lblSName.Text = drStudent["StudentName"].ToString();
    tcSNum.Controls.Add(lblSNum);
    tr.Cells.Add(tcSNum);
    t.Rows.Add(tr);
    i++;
    }
    this.pnl.Controls.Add(t);
    
    Below code to find controls in the Panel
    
    for(int i = 0;i<numberofStudents;i++)
    {
    Label lblrno = (Label)pnl.FindControl("LBLSNAME" + i.ToString());
    Response.Write(lblrno.Text);
    }
    
    

    Please help me in this regard.

    Thanks in advance


    Thanks & Regards Prasad DVR

    • Moved by Mike Dos Zhang Friday, May 25, 2012 4:00 AM asp.net topic (From:Visual C# General)
    Thursday, May 24, 2012 7:57 AM

Answers

  • All controls are disposed when Page life cycle ends and if you don't somehow safe the state of the controls, those controls will not be available after the post back.

    I recommend you post this question to ASP.NET forums or search similar problems from there. There is many posts about handling dynamic controls and page life cycle in right manner.

    • Proposed as answer by Mr. Wharty Wednesday, May 30, 2012 2:27 AM
    • Marked as answer by Mr. Wharty Tuesday, June 5, 2012 11:11 PM
    Thursday, May 24, 2012 8:22 AM