Click here to Skip to main content
16,006,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am adding different controls according to value stored in db. eg if the value is 1 i am adding textbox and if its 0 i am adding drpdwnlist.

my code is as below:

C#
cmd = new SqlCommand("Select label,control_name FROM search", connection);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        var table = new Table();
      
        for (int x = 0; x < dt.Rows.Count; x++)
        {
            string contrl = dt.Rows[x]["control_name"].ToString();
                 if (contrl.Equals("1"))
            {
                var row = new TableRow();
                var cell = new TableCell();
                var cell1 = new TableCell();
                TextBox textbox = new TextBox();
                textbox.ID = "txt"+ x;
               
                textbox.Width = new Unit(180);

                cell.Controls.Add(textbox);
                row.Cells.Add(cell1);
                row.Cells.Add(cell);
                cell1.Text = lbl;

                if (dts.Rows[0][x].Equals("0"))
                    table.Rows.Add(row);
            }
            if (contrl.ToString().Equals("0"))
            {
                var row = new TableRow();
                var cell = new TableCell();
                var cell1 = new TableCell();
                DropDownList ddlPercent = new DropDownList();
                ddlPercent.AutoPostBack = true;
                ddlPercent.ID ="ddlPercent "+x;

                cell.Controls.Add(ddlPercent);
                row.Cells.Add(cell1);
                cell1.Text = lbl;
                row.Cells.Add(cell);
                table.Rows.Add(row);
            }

 container.Controls.AddAt(0, table);

i am using following code to find added controls but its not working n giving errer object reference is not set

C#
object wantedNode = this.FindName("txt"+x);
            if (wantedNode is TextBox)
            {
                
                TextBox wantedChild = wantedNode as TextBox;
                 a = wantedChild.Text;
                
            }

Please help me in this regard.

Thanks in advance
Posted
Updated 2-Jan-14 18:20pm
v2

1 solution

Hi try adding your dynamic controls in Page_Init method, It will work...

C#
protected void Page_Init(object sender, EventArgs e)
       {
           TextBox txt = new TextBox () { ID="txt"};
           divContainer.Controls.Add(txt);
       }



C#
protected void Button1_Click(object sender, EventArgs e)
       {
          var text =  divContainer.FindControl("txt") as TextBox;
          text.Text = "modified";
       }
 
Share this answer
 
Comments
Member 10060795 3-Jan-14 2:38am    
thanks but i have to add dyamic controls on selected_indexchng event.
can you please help in this regard.
Karthik_Mahalingam 3-Jan-14 4:15am    
like the same, you have to add your dynamic controls in the pageinit method.
if have to use ispostback condition to check, when to add the controls..
Member 10060795 3-Jan-14 8:35am    
thanks my problem is solved now
Karthik_Mahalingam 3-Jan-14 8:41am    
welcome :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900