C# asp.net looping thru labels dynamic assign text...

howdy experts...

I am an ASP coder converting to C# and asp.net so pretty new to this (experience with c, c++ and java).

I built a template page with 12 labels formated in an html table.  In my code page I want to loop thru thru these labels and add data from a database.  I wont know exactly how much data there is so i have a next and prev button on the bottom to cycle thru the data.

i am using a foreach (control control in form1.controls) loop to loop thru the controls in my form.  I check to see if control.id is equal to the string "label"+index (index is an int variable that i just increment).  If I find the right label i want to add a string to that label (the string is basically some html with the db data... links, and image, and some text).

First off am i correct in using a label instead of a placeholder?  I couldnt seem to get the string into the placeholder properly so that is why i use label.

Secondly how to assign the string to "found" label.  I know the variable control probably holds the reference to the label but how do i set the text attribute = to my string?

This is probably pretty simple but time constrants and feature creep are driving me batty... ;)

thanks
razzzzerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
If these are <asp: Label ID="lbl1" runat="server"> then they will have the runat="server" tag and you can assign it a value in the code behind as follows: this.lbl1.Text = "my new text";
if it is an html label, add the runat="server" and you should be able to reference it the same.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
razzzzerAuthor Commented:
thanks for the speedy response xD

because i get the the id from that foreach loop how do i replace lbl1 with the dynamically drerived ID?
0
razzzzerAuthor Commented:
essentially im looking for something like the javascript eval function....

eval("label"+index).text = string....

or a way to assign via that control variable assigned in the foreach loop....

thanks
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Lonny_Commented:
foreach(Control c in this.Controls)
{
      if(c is Label)
            c.Text = "The Text";
}
0
razzzzerAuthor Commented:
i get a system.web.ui.control does not contain a definition for 'text' in the assignment .  if i replace the assignment with a response.write(c.id) the name of the label is printed out so the loop IS finding the right control..... any other ideas?

thanks
rich
0
Lonny_Commented:
Try this.

Label l;
foreach(Control c in this.Controls)
{
      if(c is Label)
      {
            l = (Label)c;
            l.Text;
      }
}

Best Regards.
0
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
If this is an html label with runat="server" you would probably want to use c.InnerText = "The Text";
0
Lonny_Commented:
Label l;
foreach(Control c in this.Controls)
{
      if(c is Label)
      {
            l = (Label)c;
            l.Text = "The Text";
      }
}
0
razzzzerAuthor Commented:
xD-
no this is an asp label... tried the "InnerText" anyway gave me a similiar no definition error...

Lonny-
i tried the casting earlier and got an error... tried it again your way and got another error... odd one tho
this time it says System.Web.UI.WebControls.Label does not contain a definition for 'text" but if i change the assignment to the static lbl1.text = string... it works fine.... hmmmm

0
razzzzerAuthor Commented:
omg....
case sensitive!!!!!!!!!!!!!!!!!!!!!!!!!
0
razzzzerAuthor Commented:
feel like an idiot hehe..... text vs Text......

had it right all along but thanks for helping me weed thru it!
0
razzzzerAuthor Commented:
Thanks... xD had the first "solution" with the capitalized Text
Lonny gave me detailed code (again with a capital Text)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.