please help me in doing this

Dani AI

Generated

You can create any number of server Labels at runtime, but the key is when and where you add them. Add them to a container (PlaceHolder or Panel) and recreate them on every request, in Page_Init or earlier, so ViewState/events are wired up. is right about using a PlaceHolder; the specific while condition shown (0 == (x - 1)) will not run when x starts at 5, which is why nothing appears. ’s loop works conceptually; adding to a dedicated container instead of form1 gives you precise placement.

Example pattern (code-behind):

protected void Page_Init(object sender, EventArgs e)
{
    // Example: get n from config, DB, or validated user input.
    int n = Math.Max(0, Math.Min(GetDesiredCount(), 200)); // cap to avoid overload

    foreach (int i in Enumerable.Range(1, n))
    {
        var lbl = new Label { ID = "lbl" + i, Text = "Label " + i };
        phLabels.Controls.Add(lbl);
        phLabels.Controls.Add(new LiteralControl("<br />")); // spacing
    }
}

Practical tips:

  • Do not wrap the creation in if (!IsPostBack). Dynamic controls must be recreated on each request, otherwise they disappear on postback and lose state.
  • Use unique IDs (lbl1, lbl2, ...) so you can find them later with phLabels.FindControl("lbl3").
  • Labels render inline; add breaks (as above) or apply a CSS class with display:block;.
  • If n comes from user input, validate and cap it to prevent creating thousands of controls unintentionally.
  • If you only need to output text and do not reference the controls on the server, consider Literal or plain HTML to reduce server-control overhead. For large lists, a Repeater/ListView is often cleaner than hand-adding many Labels.

Recommended Answers

All 6 Replies

that is i want to create n number of labels using C# coding

I wrote this without being able to test it, but it should at least get you started.
Insert a placeholder into the HTML where you want the Labels to appear, then use this code.

{
    int x = 5;

    while (0 == (x - 1)) {
        Label myLabel = new Label();

        myLabel.ID = "myLabel_" + x;
        myLabel.Text = "Label No. " + x;

        PlaceHolder1.Controls.Add(myLabel);
        x += x;
    }
}
for(int i=0;i<n;i++)
{
Label NewLabel = new Label();
NewLabel.ID ="Label"+i;
NewLabel.Text = "Label" + i;
this.form1.Controls.Add(NewLabel);
}

thanks geniusvishal & TimCadieux :):)

static int SumWhile()
{
    //
    // Sum numbers 0 .. 4
    //
    int sum = 0;
    int i = 0;
    while (i < 5)
    {
    sum += i;
    i++;
    }
    return sum;
}
using System;

class Program
{
    static void Main()
    {
    //
    // Shows five 'for int i' loops.
    //
    Console.WriteLine("--- For 1 ---");
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(i);
    }
    Console.WriteLine("--- For 2 ---");
    for (int i = 10 - 1; i >= 0; i--)
    {
        Console.WriteLine(i);
    }
    Console.WriteLine("--- For 3 ---");
    for (int i = 0; i < 10; i += 2)
    {
        Console.WriteLine(i);
    }
    Console.WriteLine("--- For 4 ---");
    for (int i = 10 - 1; i >= 0; i -= 2)
    {
        Console.WriteLine(i);
    }
    Console.WriteLine("--- For 5 ---");
    for (int i = 0; i < (20 / 2); i += 2)
    {
        Console.WriteLine(i);
    }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.