Add new line within textbox and label from code behind c#

3k views Asked by At

Here is my code for creating the textbox and label from code

        int i = 0;
        Label lbl = new Label();
        lbl.Text = "Label - " + i.ToString();
        lbl.ID = "Label - " + i.ToString();

        TextBox txt = new TextBox();
        txt.Text = "txt - " + i.ToString();

        data.Controls.Add(lbl);
        data.Controls.Add(txt);

Is it possible I add a new line after the data.Controls.Add(txt);?

I tried to use Environment.NewLine but its not working. anyone have any idea how to do it?

4

There are 4 answers

0
sriman reddy On BEST ANSWER
data.Controls.Add(new LiteralControl("<br/>"));

Add this for new line

0
Nila On

Are you saying the space between the two controls.. If this is the case, you can set the location for the next control such that there is a empty space after that text.

3
Chandan Kumar On

Add a Literal Control in between your two controls for line space. Programatically you can do it.

Set Literal Control's Mode property to PassThrough For Example :

Literal myLiteral = new Literal();
myLiteral.Id="lit_newline"
myLiteral.Mode = "PassThrough";
data.Controls.Add(myLiteral);

Then Add this control in between your textbox and label control

Also you can do like this

data.Controls.Add(new LiteralControl("<br />")); 

And If you want, you can apply css to your textbox without adding the literal control which will increase the height - as answered by user : bgs264

0
bgs264 On

You should use CSS if it's purely for presentation.

Add .CssClass = "TextboxSpace" to the textbox
Add a css file to the project (or add a new style to an existing one) something like

.TextboxSpace {
 margin-bottom: 20px;
}