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

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?

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

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;
}

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

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.

Related

User Control TextBox with Label

How to make a user control which extend from TextBox ,and add a Label beside the textbox , not inside the textbox , the label must be beside it or on top of it.
BTW the user control must extend from TextBox .
For long time I have been searching for this problem and I cant find any anwser.
If I'm understanding you correctly, you want to place a Label next to a TextBox. If you want to do this with code, it's as simple as:
int x, y;
x = y = 200;
TextBox tb = new TextBox();
tb.Width = 100;
tb.Left = x;
tb.Top = y;
Label lbl = new Label();
lbl.Width = 50;
//If you want it on the right of the TextBox
lbl.Left = tb.Right + 10;
//If you want it on the left of the TextBox
lbl.Right = tb.Left - 10;
You can obviously modify all of those values to your hearts desire. Also, this seems kind of moot, because if you're going to be doing this in a WinForms Application, you can just drag and drop everything you want in to place. You're obviously new to this... Check out some YouTube on how to create WinForms Applications and get on MSDN...
https://www.youtube.com/watch?v=DdXrw6HUzCA
https://msdn.microsoft.com/en-us/dn308572.aspx

Dynamically added labels disappear in runtime

I add labels to my form programmatically, but they disappear, except last one. I'm sure that given location to them is appropriate. But when the second label appears, first disappears, or when third label appears, second disappears.
Here is my code:
Label[] lenlab = new Label[255];
Label lab = new Label();
lab.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
lab.ForeColor = Color.White;
lab.BackColor = Color.Transparent;
lab.AutoSize = true;
lenlab[1] = lab;
lenlab[1].Location = new Point(50, panel1.Location.Y + panel1.Height + 20);
lenlab[1].Text = c[1];
this.Controls.Add(lenlab[1]);
for (int i = 2; i < c.Count; i++)
{
lenlab[i] = lab;
lenlab[i].Location = new Point(lenlab[i - 1].Location.X + lenlab[i -1].Width + 40, lenlab[i - 1].Location.Y);
lenlab[i].Text = " + " + c[i];
this.Controls.Add(lenlab[i]);
}
This line is causing every position in your array to have a reference to the same Label you created originally, outside the loop, which means all you're doing is changing the position and text of the same Label inside your loop.
lenlab[i] = lab;
The behavior you're seeing is due to the fact that you can only add a particular control to this.Controls once, so the effect is that you see the same label changing position.
Here's the portion of the Add() method that checks whether the control you're adding already has a parent, and if it does, then it removes it from it's current parent before adding it to the new one. So every time you call this.Controls.Add() with the same Label, it removes it from the Form and then adds it again.
// Remove the new control from its old parent (if any)
if (value.parent != null) {
value.parent.Controls.Remove(value);
}
Instead, create a new Label inside your for loop:
lenlab[i] = new Label();
There are controls that can help you layout controls without the need to calculate a new position each time. In particular, read up on the FlowLayoutPanel and TableLayoutPanel classes.
What you are doing there is basically create one Label, change it several times, and attach it several times to the page. What you end up having on the page is the last version of the Label being added once, which is the expected behavior.
If you want to add several labels, you need to new each of them.

how do you create an <h1> tag programmatically in ASP.NET?

I'm trying to generate some html programmatically in my code behind for a user control I'm designing.
I've been looking around, but can't seem to figure out how to dynamically generate some h1 tags for content I'll be displaying.
Is it just a Label with a special property set?
var h1 = new HtmlGenericControl("h1");
h1.InnerHtml = "header content";
You could also use:
<h1 runat="server" />
You can use label or literal controls as shown below:
Label1.Text = "<h1>HI All</h1>";
Or
string heading = "HI All";
Label1.Text = string.Format("<h1>{0}</h1>", heading);
You can do this easily by
literall.Text ="<h1>ABCD</h1>";
Let, you have a server control like this,
<div class="cls" runat="server" id="MyServerControlDiv"></div>
using HtmlGenericControl
var h1 = new HtmlGenericControl("h1");
h1.InnerHtml = "header content";
MyServerControlDiv.Controls.Add(h1);
using LiteralControl
MyServerControlDiv.Controls.Add(new LiteralControl("<h1>"));
MyServerControlDiv.Controls.Add(new LiteralControl("header content"));
MyServerControlDiv.Controls.Add(new LiteralControl("</h1>"));
Use Literal control, Literal1.Text = "<h1>" + texttodisplay + "</h1>";
ASP literals may help you.
http://ganeshmohan.wordpress.com/aspnet-and-c-generate-html-controls-dynamically/
Personally I prefer to make a class for it.
public class H1: HtmlGenericElement
{
public H1(): base("h1") { }
}
Or maybe.
public class H: HtmlGenericControl
{
public H(int size): base("h" + size) { }
}
Let us assume that you have an asp placeholder in your design:
PlaceHolder placeHolder1 = new PlaceHolder();
string yourText = "Header Text";
Label myLabel = new Label();
myLabel.Text = "<h1>" + yourText + "</h1>";
PlaceHolder1.Controls.Add(myLabel);
OR
PlaceHolder1.Controls.Add(new LiteralControl ("<h1>")) // and likewise
for </h1>.
Using a generic HTML control or a literal control is fine just so long as you don't want to insert any child controls. If you do need to append a child control such as a label for example, then you will want to create you header as a Web Control like follows:
WebControl H2Header = new WebControl(HtmlTextWriterTag.H1);
And then you can append a child control like this:
Label labHeader = new Label() { Text = "Some Header Text" };
H2Header.Controls.Add(labHeader);
What i always prefer to use:
In the front end put the desired html control like
<h1 id="title" runat="server">XXX</h1>
Then in the behind code declare the desired control like
protected HtmlGenericControl PublicTitle
{
get
{
return this.title;
}
}
And anywhere in the code play whith the control
PublicTitle.InnerHtml = state.Equals("win") ? "Win" : "Lost";
PublicTitle.Attributes.Add("src", "https://xxxx");

C# Label not visible in GroupBox

I have a loop that is supposed to go through DataTable and for each row create a new GroupBox, set it's text to value from one column, in that GroupBox I want to put a Label with Text similar to another column in the table.
This is just part of the code!
for (int i = 0; i < tab.Rows.Count; i++)
{
lblbox[i] = new GroupBox();
lblbox[i].Text = tab.Rows[i]["text"].ToString();
lblbox[i].Name = "box no " + i.ToString();
lblbox[i].Visible = true;
this.Controls.Add(lblbox[i]);
lblbox[i].Location = new Point(5, 55 * i);
lblbox[i].Height = 50;
lblbox[i].SendToBack();
importancelbl[i] = new Label();
importancelbl[i].Text = "Importance: " + tab.Rows[i]["importance"].ToString();
importancelbl[i].Name = "implbl" + i.ToString();
importancelbl[i].Visible = true;
lblbox[i].Controls.Add(importancelbl[i]);
importancelbl[i].BringToFront();
Point locP = new Point();
locP.X = lblbox[i].Location.X + 5;
locP.Y = lblbox[i].Location.Y + 15;
importancelbl[i].Location = locP;
}
When i run the code it creates three (I have three rows in my table) GroupBoxes correctly and creates all the labels, but only first label is visible in its Groupbox. When I add those labels to the Form and not to the GroupBox, all of them are visible, but I want them to be in boxes...
I've tried pretty much everything and I'm still very confused (espacially by the behavior of the first label). I know the mistake is probably obvious and stupid, but I just can't find it!
Control.Location is relative to its parent, so set Location for the label to (5, 15).
locP.X = 5;
locP.Y = 15;
My guess is that they are somehow overlapping and making each other disappear somehow.
Could you try posting pictures of the form when it works and when it doesn't? Also add all your code?
Try to preform adding
lblbox[i].Controls.Add(importancelbl[i]);
this.Controls.Add(lblbox[i]);
after setting all your properties

Created LinkButton using C# code not working

I have created link button using c# code but these are not click able why?
This is c# code
<% {
List<string> PMlist = new List<string>();
PMlist = PManifacutrerList;
foreach (string PM in PMlist)
{
Response.Write(PM);
}
}
%>
And following code is used to add list li in the PMlist
PMList.Add(
"<li><asp:LinkButton ID=\"LinkButton1\" style=\"color: Blue;font-family: Microsoft New Tai Lue; text-decoration: none;\" runat=\"server\">" +
ds.Tables[0].Rows[i]["PM_name"].ToString() + "</asp:LinkButton></li>");
Your Response.Write(PM) is simply writing HTML to the response output. If you really want to use a LinkButton, you need to create an instance:
LinkButton lb = new LinkButton();
lb.Text = "click me";
lb.Click += new EventHandler(delegate (object s, EventArgs ev) {
// handle click event
});
form1.Controls.Add(lb);
If you don't need a server post back, then you can just use a simple link such as:
PMList.Add(
"<li><a href='#' style='color: Blue;font-family: Microsoft New Tai Lue; text-decoration: none;'>" + ds.Tables[0].Rows[i]["PM_name"].ToString() + "</a></li>");
Hope it helps!
You need to write OnClick event
In order to dynamically add a control there must be a container.
If you don't have a container on page you can a placeholder
control & add controls to it
You must create an instance of control to add it onto page
Label myLabel = new Label();
myLabel.Text = "Sample Label";
myPlaceHolder.Controls.Add(myLabel);
Adding Controls dynamically:MSDN

Categories