Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. create label in the run time

create label in the run time

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdata-structurestutorial
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rmedo
    wrote on last edited by
    #1

    i want to know how to create array of labels in the run time and set it size and position in the ASP.NET

    B M 2 Replies Last reply
    0
    • R rmedo

      i want to know how to create array of labels in the run time and set it size and position in the ASP.NET

      B Offline
      B Offline
      Bill Dean
      wrote on last edited by
      #2

      Have you tried adding the label to the Page's Controls collection? It would look something like:this.LabelArray[i]=new Label(); LabelArray[i].Text = "Some Message"; LabelArray[i].Height = 10; LabelArray[i].Width = 50; this.Controls.Add(LabelArray[i]);
      Where i is an int and you have already declared the the LabelArray as a private member of the page. I do not believe that you can set position directly at run-time. Instead, you can add LiteralControls between adding Labels, like this:this.Controls.Add(new LiteralControl(""); this.Controls.Add(new LiteralControl(""); this.Controls.Add(new LiteralControl(""); this.Controls.Add(new LiteralControl(""); this.Controls.Add(new LiteralControl(""); this.Controls.Add(new LiteralControl(" "); this.Controls.Add(LabelArray[0]); this.Controls.Add(new LiteralControl(" "); this.Controls.Add(LabelArray[1]); this.Controls.Add(new LiteralControl(" ");
      As an aside, when I have to do something this involved, I usually do it in a custom web control. Hope this helps, Bill

      1 Reply Last reply
      0
      • R rmedo

        i want to know how to create array of labels in the run time and set it size and position in the ASP.NET

        M Offline
        M Offline
        Mike Ellison
        wrote on last edited by
        #3

        Hi there. Here's a sample.

        <script language="C#" runat="server">
        void Page_Load(object o, EventArgs e)
        {
        // create array of labels
        const int kITEMS = 10;
        Label[] arr = new Label[kITEMS];
        for (int i=0; i<kITEMS; i++)
        {
        // create the label
        arr[i] = new Label();

          // set label text and size properties
          arr\[i\].Text = string.Format("This is Label #{0}", i);            
          int iSize = 8 + (i \* 2);
          arr\[i\].Font.Size = new FontUnit(iSize);
          
          // set absolute position through css style attributes
          arr\[i\].Style\["position"\] = "absolute";
          arr\[i\].Style\["left"\] = string.Format("{0}px", (i \* 20) + 20);
          arr\[i\].Style\["top"\] = string.Format("{0}px", (i \* 30) + 50);
        
          
          // add to the placeholder on the page
          myPlaceHolder.Controls.Add(arr\[i\]);
        }
        
        // ... perhaps do something else with the array ...
        

        }

        </script>

        <html>
        <head>
        </head>
        <body style="font-family: Tahoma">
        <form runat="server">

            <b>Labels generated at Runtime</b><hr/>
        
            <%-- placeholder for runtime-created labels --%>
            <asp:PlaceHolder runat="server" id="myPlaceHolder" />
        
        </form>
        

        </body>
        </html>

        B 1 Reply Last reply
        0
        • M Mike Ellison

          Hi there. Here's a sample.

          <script language="C#" runat="server">
          void Page_Load(object o, EventArgs e)
          {
          // create array of labels
          const int kITEMS = 10;
          Label[] arr = new Label[kITEMS];
          for (int i=0; i<kITEMS; i++)
          {
          // create the label
          arr[i] = new Label();

            // set label text and size properties
            arr\[i\].Text = string.Format("This is Label #{0}", i);            
            int iSize = 8 + (i \* 2);
            arr\[i\].Font.Size = new FontUnit(iSize);
            
            // set absolute position through css style attributes
            arr\[i\].Style\["position"\] = "absolute";
            arr\[i\].Style\["left"\] = string.Format("{0}px", (i \* 20) + 20);
            arr\[i\].Style\["top"\] = string.Format("{0}px", (i \* 30) + 50);
          
            
            // add to the placeholder on the page
            myPlaceHolder.Controls.Add(arr\[i\]);
          }
          
          // ... perhaps do something else with the array ...
          

          }

          </script>

          <html>
          <head>
          </head>
          <body style="font-family: Tahoma">
          <form runat="server">

              <b>Labels generated at Runtime</b><hr/>
          
              <%-- placeholder for runtime-created labels --%>
              <asp:PlaceHolder runat="server" id="myPlaceHolder" />
          
          </form>
          

          </body>
          </html>

          B Offline
          B Offline
          Bill Dean
          wrote on last edited by
          #4

          I stand corrected. Thanks Mike.

          M 1 Reply Last reply
          0
          • B Bill Dean

            I stand corrected. Thanks Mike.

            M Offline
            M Offline
            Mike Ellison
            wrote on last edited by
            #5

            Hey, Bill. I liked your approach too - I think I posted mine at about the same time you posted yours.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups