We help IT Professionals succeed at work.

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

razzzzer
razzzzer asked
on
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
Comment
Watch Question

Software Applications Developer / Integrator
CERTIFIED EXPERT
Commented:
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.

Author

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?

Author

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

Commented:
foreach(Control c in this.Controls)
{
      if(c is Label)
            c.Text = "The Text";
}

Author

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

Commented:
Try this.

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

Best Regards.
Daniel ReynoldsSoftware Applications Developer / Integrator
CERTIFIED EXPERT

Commented:
If this is an html label with runat="server" you would probably want to use c.InnerText = "The Text";
Commented:
Label l;
foreach(Control c in this.Controls)
{
      if(c is Label)
      {
            l = (Label)c;
            l.Text = "The Text";
      }
}

Author

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

Author

Commented:
omg....
case sensitive!!!!!!!!!!!!!!!!!!!!!!!!!

Author

Commented:
feel like an idiot hehe..... text vs Text......

had it right all along but thanks for helping me weed thru it!

Author

Commented:
Thanks... xD had the first "solution" with the capitalized Text
Lonny gave me detailed code (again with a capital Text)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.