create label in the run time
-
i want to know how to create array of labels in the run time and set it size and position in the ASP.NET
Have you tried adding the label to the Page's Controls collection? It would look something like:
this.LabelArray[i]=new Label(); LabelArray[i].Text = "Some Message"; LabelArray[i].Height = 10; LabelArray[i].Width = 50; this.Controls.Add(LabelArray[i]);
Where i is an int and you have already declared the the LabelArray as a private member of the page. I do not believe that you can set position directly at run-time. Instead, you can addLiteralControl
s between addingLabels
, like this:this.Controls.Add(new LiteralControl(""); this.Controls.Add(new LiteralControl(""); this.Controls.Add(new LiteralControl(""); this.Controls.Add(new LiteralControl(""); this.Controls.Add(new LiteralControl(""); this.Controls.Add(new LiteralControl(" "); this.Controls.Add(LabelArray[0]); this.Controls.Add(new LiteralControl(" "); this.Controls.Add(LabelArray[1]); this.Controls.Add(new LiteralControl(" ");
As an aside, when I have to do something this involved, I usually do it in a custom web control. Hope this helps, Bill -
i want to know how to create array of labels in the run time and set it size and position in the ASP.NET
Hi there. Here's a sample.
<script language="C#" runat="server">
void Page_Load(object o, EventArgs e)
{
// create array of labels
const int kITEMS = 10;
Label[] arr = new Label[kITEMS];
for (int i=0; i<kITEMS; i++)
{
// create the label
arr[i] = new Label();// set label text and size properties arr\[i\].Text = string.Format("This is Label #{0}", i); int iSize = 8 + (i \* 2); arr\[i\].Font.Size = new FontUnit(iSize); // set absolute position through css style attributes arr\[i\].Style\["position"\] = "absolute"; arr\[i\].Style\["left"\] = string.Format("{0}px", (i \* 20) + 20); arr\[i\].Style\["top"\] = string.Format("{0}px", (i \* 30) + 50); // add to the placeholder on the page myPlaceHolder.Controls.Add(arr\[i\]); } // ... perhaps do something else with the array ...
}
</script>
<html>
<head>
</head>
<body style="font-family: Tahoma">
<form runat="server"><b>Labels generated at Runtime</b><hr/> <%-- placeholder for runtime-created labels --%> <asp:PlaceHolder runat="server" id="myPlaceHolder" /> </form>
</body>
</html> -
Hi there. Here's a sample.
<script language="C#" runat="server">
void Page_Load(object o, EventArgs e)
{
// create array of labels
const int kITEMS = 10;
Label[] arr = new Label[kITEMS];
for (int i=0; i<kITEMS; i++)
{
// create the label
arr[i] = new Label();// set label text and size properties arr\[i\].Text = string.Format("This is Label #{0}", i); int iSize = 8 + (i \* 2); arr\[i\].Font.Size = new FontUnit(iSize); // set absolute position through css style attributes arr\[i\].Style\["position"\] = "absolute"; arr\[i\].Style\["left"\] = string.Format("{0}px", (i \* 20) + 20); arr\[i\].Style\["top"\] = string.Format("{0}px", (i \* 30) + 50); // add to the placeholder on the page myPlaceHolder.Controls.Add(arr\[i\]); } // ... perhaps do something else with the array ...
}
</script>
<html>
<head>
</head>
<body style="font-family: Tahoma">
<form runat="server"><b>Labels generated at Runtime</b><hr/> <%-- placeholder for runtime-created labels --%> <asp:PlaceHolder runat="server" id="myPlaceHolder" /> </form>
</body>
</html> -
Hey, Bill. I liked your approach too - I think I posted mine at about the same time you posted yours.