• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

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
0
razzzzer
Asked:
razzzzer
  • 7
  • 3
  • 2
2 Solutions
 
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now