Home > DeveloperSection > Forums > Can I loop through textbox id’s to check values in asp.net
Pravesh Singh

Total Post:411

Posted on    November-14-2014 2:21 AM

 ASP.Net C#  Loop 

 1 Reply(s)
 597  View(s)
Rate this:

I'm working on a project and I'm a beginner and I'm having a little trouble with this. I'm trying to check if a textbox is empty and if it is to change the value to N/A so that I can input n/a into a database instead of it not working.

Here is the code that I thought would work but didn't because the .Text property isn't near the ID anymore:

for(int i = 1; i<=17; i++)


if(!("tb" + i).Text)

"tb" + i.Text = "n/a";


I wasn't sure if the true/false would work but I never got to find out because it doesn't compile to begin with. I have 17 textboxes on my design page all with ID 'tb + i' e.g tb1, tb2

Samuel Fernandes
Samuel Fernandes

Total Post:155

Posted on    November-14-2014 6:16 AM

If you want to loop through the textbox ids, you have to find the control on the page first using the FindControl method.

Then you can create your loop like this:

TextBox txt;

for(int i = 1; i<=17; i++)


    txt = (TextBox)Page.FindControl("tb" + i);


        txt.Text = "n/a";


Don't want to miss updates? Please click the below button!

Follow MindStick