Dynamic Label Not Being Added

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jagdeep gupta
    New Member
    • Aug 2010
    • 98

    Dynamic Label Not Being Added

    Thanks a lot friend but a problem does occur is i have added a br tag as well but it is not shown in output plz check:
    Code:
    TextBox [] textBoxArr;
    Label []lbl=new Label[5];
    Label lblbr=new Label();
     for(int i = 0; i < textBoxArr.Length - 1; i++)
            {   textBoxArr(x) = New TextBox();
                textBoxArr(x).ID = "myTextBox" + x.ToString();
    lbl[i]=new Label();
    lbl.Id="myLabel" + i.ToString();
    lbl[i].Text="myLabel";
    lblbr.Text="<br/>";
                textBoxArr[i].Visible = True;
    lbl[i].Visible = True;
    lblbr.Visible=True;
     //Initializing the TextBox so that it is not rendered in the browser 
                Pnl_TextBoxes.Controls.Add(textBoxArr[i]); //Adding the TextBox to the Panel that holds the text 
     Pnl_TextBoxes.Controls.Add(lbl[i]);
     Pnl_TextBoxes.Controls.Add(lblbr);
    
            }

    label and Text Boxes Get added But Break is not There after Addition of these two
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You only have 1 lblbr defined....ther efore only one label with "<br>" in it will be added to the page. You probably don't notice it because it's at the bottom (under all of the controls).


    A Panel control is rendered as an HTML <div> element. These elements are used to group things together. The <div> is placed "on-the-next-line" and anything that comes after the <diV> is also placed "on-the-next-line".

    So, I recommend that you put the Textbox and Label within their own Panel and add that Panel to the Pnl_TextBoxes. This will group the TextBox and Label together...and will put each grouping on their own line.

    Code:
    for(int i = 0; i < textBoxArr.Length - 1; i++)
    {  textBoxArr(x) = New TextBox();
       textBoxArr(x).ID = "myTextBox" + x.ToString();
       lbl[i]=new Label();
       lbl.Id="myLabel" + i.ToString();
       lbl[i].Text="myLabel";
    //   lblbr.Text="<br/>";
       textBoxArr[i].Visible = True;
       lbl[i].Visible = True;
    //   lblbr.Visible=True;
    
      Panel textBoxLabelGroup = new Panel;
      textBoxLabelGroup.ID = "textBoxLabelGroup" + i.ToString();
      textBoxLabelGroup.Controls.Add(textBoxArr[i]);
      textBoxLabelGroup.Controls.Add(lbl[i]);
    
      Pnl_TextBoxes.Controls.Add(textBoxLabelGroup); 
    }
    Last edited by Frinavale; Sep 13 '10, 01:35 PM.

    Comment

    Working...