Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. problem finding control and getting its values for dynamically created textboxes

problem finding control and getting its values for dynamically created textboxes

Scheduled Pinned Locked Moved C#
helpdatabase
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dhyanga
    wrote on last edited by
    #1

    Hi, I have a situation where I need to show data from database to textboxes. These textboxes has to be dynamically created and can be editable. I am able to create textboxes as follows:

    string[] myData = liD1.ToArray(); //liD1 is a list that has values from database
    public void showData()
    {
    for (int i = 0; i < myData.Length; i++)
    {
    if (txtMore.FindControl("txtD1StartTime" + i) == null)
    {
    txtStart1 = new TextBox();

    		lblStart1 = new Label();
    							   
    		txtStart1.ID = "txtD1S" + i;
    								 
    		lblStart1.ID = "lblD1S" + i;
    								  
    		txtStart1.Text = myData\[i\].ToString();
    									
    		lblStart1.AssociatedControlID = txtStart1.ID;
    		
    		lblStart1.Text = "Data" + i;
    							 
    		txtMore.Controls.Add(lblStart1);
    		
    		txtMore.Controls.Add(txtStart1);
    					  
    	}
    }
    

    }

    In my ascx page, I am loading those textboxes in as follows:

    showData() function is loading data with dynamically created textboxes. This is working fine. Now If i edit textboxes and try to get new data, it is not giving me anything. Actually it couldn't even find that control. Code is as shown below:

    public void getData()
    {
    for (int i = 0; i < myData.Length; i++)
    {
    TextBox t = txtMore.FindControl("txtD1S" + i) as TextBox;

    	if (t != null)
    	{
    		string temp = t.Text;
        }
    }
    

    }

    Even though I have dynamically loaded textboxes from showData(), in getData(),it is not able to find any control. Please help.

    Dhyanga

    Richard DeemingR 1 Reply Last reply
    0
    • D Dhyanga

      Hi, I have a situation where I need to show data from database to textboxes. These textboxes has to be dynamically created and can be editable. I am able to create textboxes as follows:

      string[] myData = liD1.ToArray(); //liD1 is a list that has values from database
      public void showData()
      {
      for (int i = 0; i < myData.Length; i++)
      {
      if (txtMore.FindControl("txtD1StartTime" + i) == null)
      {
      txtStart1 = new TextBox();

      		lblStart1 = new Label();
      							   
      		txtStart1.ID = "txtD1S" + i;
      								 
      		lblStart1.ID = "lblD1S" + i;
      								  
      		txtStart1.Text = myData\[i\].ToString();
      									
      		lblStart1.AssociatedControlID = txtStart1.ID;
      		
      		lblStart1.Text = "Data" + i;
      							 
      		txtMore.Controls.Add(lblStart1);
      		
      		txtMore.Controls.Add(txtStart1);
      					  
      	}
      }
      

      }

      In my ascx page, I am loading those textboxes in as follows:

      showData() function is loading data with dynamically created textboxes. This is working fine. Now If i edit textboxes and try to get new data, it is not giving me anything. Actually it couldn't even find that control. Code is as shown below:

      public void getData()
      {
      for (int i = 0; i < myData.Length; i++)
      {
      TextBox t = txtMore.FindControl("txtD1S" + i) as TextBox;

      	if (t != null)
      	{
      		string temp = t.Text;
          }
      }
      

      }

      Even though I have dynamically loaded textboxes from showData(), in getData(),it is not able to find any control. Please help.

      Dhyanga

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You've already posted this question in QA: problem with dynamicall loaded textbox values[^] Don't post the same question in multiple places on the same site.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups