Rares
Rares

Reputation: 97

dynamic textbox near label in C#

Sorry for reposting this but I haven't figure out how to do it.

string str = string.Empty;

 foreach (ListItem item in this.CheckBoxList1.Items)
    {
       if (item.Selected)
           {
              str += item.Value + "<br><br>";
              TextBox txt1 = new TextBox();
              form1.Controls.Add(txt1);

           }
    }

 lbl1.Text = str;

This brings up a textbox each time I check a value but I want to bring the textbox near each selected value that I retain in the label. So value1 textbox1, value2 textbox2....It's asp.net web application.

Upvotes: 0

Views: 458

Answers (2)

Gnani
Gnani

Reputation: 465

Why do you want to use the same label?

U could do:

foreach (ListItem item in this.CheckBoxList1.Items)
{
   if (item.Selected)
       {
          Label label = new Label();
          label.Text = item.Value;
          TextBox txt1 = new TextBox();

          form1.Controls.Add(label);
          form1.Controls.Add(txt1);

       }
}

you also might have to assign id to the textboxes if you are planning to retrieve user input from them.

But if you only have limited number of fields u could use static fields, and change its visibility as Tim stated

Upvotes: 1

Six Beacon Gaia
Six Beacon Gaia

Reputation: 170

I don't really know what you plan to do but why dont you define a CSS class for both the label object and the textbox object using the same y- but alternate x-coordinates.

Upvotes: 0

Related Questions