• 0

[ASP.NET C#] Iterate through dynamic controls


Question

I have an ASP.NET C# web app that has a table dymanically created at run-time. In the table there are a number of dropdowns and textboxes created, depending on the number of Rows in the database.

My question is, how do I access these controls once the page is loaded? Here's some code I used to create them:

// makes the comments textbox and labels as txt[AccountID]
    	TextBox txt = new TextBox();
    	txt.TextMode = TextBoxMode.MultiLine;
    	txt.ID = "txt" + Convert.ToString(thisReader3.GetValue(6));
    	tcell.BorderColor = Color.FromArgb(215, 215, 255); // light blue
    	tcell.BorderWidth = 1;
    	tcell.VerticalAlign = VerticalAlign.Top;
    	tcell.HorizontalAlign = HorizontalAlign.Right;
    	tcell.Width = 152;
    	tcell.Controls.Add(txt); 
    	trow.Cells.Add(tcell);

    	tblDynamic.Rows.Add(trow);
    	thisReader3.Read();

8 answers to this question

Recommended Posts

  • 0

Ok I found this code but it's not working... it's not finding my textbox controls on the page. Anyone know why?

void IterateThroughChildren(Control parent)
  {
  	foreach (Control c in parent.Controls)
  	{
    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
    {
    	string com = ((TextBox) c).ID.ToString();
    }
        
    if (c.Controls.Count > 0)
    {          
    	IterateThroughChildren(c);          
    }
  	}
  }

  • 0

GetType() returns the top level class only... so it's good to check for the control type.

but... if you're checking this list on PostBack, then you should know that probably your controls aren't there. you have to either REbuild them or add them to the page state, so they are persistent. runtime-created controls aren't persistent, by default.

  • 0
  Menge said:
GetType() returns the top level class only... so it's good to check for the control type.

but... if you're checking this list on PostBack, then you should know that probably your controls aren't there. you have to either REbuild them or add them to the page state, so they are persistent. runtime-created controls aren't persistent, by default.

586124298[/snapback]

Thanks! How do I add them to the page state dynamically?

  • 0
  Lazereth said:
you are better of using "is typeof(inserttypehere)" rather than using the string signature of the relevant control. You'll also note that its perhaps better to check the interface rather than the control inself.

586124289[/snapback]

Can you give me an example of how the code would look instead? Thanks!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Yeah!!!! I was damn well shocked to see the tab with previous file open when I clicked on the new file. It had private info and I was opening another file to show another person. That was weird
    • The article says the whole setup is about the size of a pencil case which sounds pretty portable to me.
    • I thought router has to have 6 Ghz band to be called wifi-7, guess I was wrong...
    • I have the Pixel 9 Pro XL...Unless this thing is "leaps and bounds" faster than the 9, I'll pass. And by leaps and bounds, I don't mean on benchmarks. "Real world" faster. Most people don't even come close to topping out the performance of their phones. Tensor G5 is Google's most powerful chip to date, boasting a staggering 36 percent performance leap over G4.
    • MIT's stunning 'bubble wrap' device squeezes water out from thin air even in deserts by Sayan Sen Image by Matteo Roman via Pexels Massachusetts Institute of Technology (MIT) engineers have built a new kind of device that can pull clean drinking water straight out of the air—no electricity needed. It’s designed for areas where water is scarce and traditional sources like rivers or lakes aren’t reliable. Right now, more than 2.2 billion people globally don’t have access to safe drinking water. In the United States alone, 46 million face water insecurity, with either no running water or water that’s not safe to drink. This new device, called an Atmospheric Water Harvesting Window (AWHW), uses a unique hydrogel panel that looks like black bubble wrap. These dome-shaped bubbles soak up water vapor from the air, especially at night when humidity is higher. During the day, sunlight makes the vapor inside evaporate. That vapor then condenses on a glass surface and drips down through a tube, turning into drinkable water. The AWHW doesn’t rely on power sources like batteries or solar panels. It’s completely passive, meaning it works on its own. The team tested a meter-sized panel in Death Valley, California, one of the driest places in North America, and got between 57.0 and 161.5 milliliters of water per day even with humidity as low as 21 percent. That’s more than what other similar passive devices have managed. “We have built a meter-scale device that we hope to deploy in resource-limited regions, where even a solar cell is not very accessible,” said Xuanhe Zhao, a professor at MIT. “It’s a test of feasibility in scaling up this water harvesting technology. Now people can build it even larger, or make it into parallel panels, to supply drinking water to people and achieve real impact.” Another cool part of the design is how they kept the water safe to drink. Usually, these kinds of hydrogels use salts like lithium chloride to absorb more vapor but that can lead to salt leaking into the water, which isn’t ideal. To solve this, MIT’s team mixed in glycerol, a compound that helps keep salt locked inside the gel. In testing, the lithium ion concentration in the harvested water stayed below 0.06 ppm (parts per million), which is way below the safe limit. The hydrogel domes also give the material more surface area, letting it collect more vapor. The outer glass panel is coated with a special polymer film that helps cool the glass, making it easier for vapor to condense. “This is just a proof-of-concept design, and there are a lot of things we can optimize,” said lead author Chang Liu, now a professor at the National University of Singapore. “For instance, we could have a multipanel design. And we’re working on a next generation of the material to further improve its intrinsic properties.” Published in Nature Water, the study says the AWHW could last at least a year and shows promise for making safe, sustainable water in places with harsh climates. The researchers believe an array of vertical panels could one day supply water to individual households, especially in remote or off-grid locations. Source: MIT News, Nature This article was generated with some help from AI and reviewed by an editor. Under Section 107 of the Copyright Act 1976, this material is used for the purpose of news reporting. Fair use is a use permitted by copyright statute that might otherwise be infringing.
  • Recent Achievements

    • One Month Later
      Ricky Chan earned a badge
      One Month Later
    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
    • One Month Later
      Ian_ earned a badge
      One Month Later
    • Dedicated
      MacDaddyAz earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      ATLien_0
      209
    3. 3
      Michael Scrip
      204
    4. 4
      Xenon
      143
    5. 5
      +FloatingFatMan
      121
  • Tell a friend

    Love Neowin? Tell a friend!