Posted by : Anwar Hossain
Category : How to create asp.net control dynamically

Dynamically Label control creation in ASP.Net C #.

Dear viewer sometimes we need to create ASP.Net Label programmatically or dynamically. In this tutorial I will show how to create ASP.Net Label Control dynamically in ASP.Net C #. For creating ASP.Net Label Control I have created a function and called the function page load event. I have used for loop to create multiple Label but without using loop one Label Control can be created.

Dynamically Label control creation in ASP.Net C #.

HTML

<html>

<head runat="server">

    <title>Create Label dynamically  </title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

    </div>

    </form>

</body>

</html>

 

CODE BEHIND

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Drawing;

 

public partial class LabelControl : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

        //Create Label dynamically

 

        CreataLabel();

    }

    private void CreataLabel()

    {

 

        //limit to Label textbox

        int a = 15;

        for (int i = 0; i < a; i++)

        {

            Label lblDynamic = new Label();

 

            //Declaring the textbox ID name

 

            lblDynamic.ID = "lbl" + "" + ViewState["MyLebel"] + i + "<br/>";

 

            lblDynamic.Width = 400;

            lblDynamic.Height = 20;

            lblDynamic.Text = "Label" + "&nbsp;" + i + "<br/>";

          

            lblDynamic.BorderStyle = BorderStyle.Solid;

            lblDynamic.Style["Top"] = "25px";

            lblDynamic.Style["Left"] = "100px";

            lblDynamic.BackColor = Color.DarkKhaki;

            lblDynamic.BorderColor = Color.DarkOliveGreen;

          

            form1.Controls.Add(lblDynamic);

        }

 

    }

 

  

}

 

 

Out Put

Label_dynamic_control



Realted Article Headline

Dynamically Label control creation in ASP.Net C #.
Dynamically TextBox control creation in ASP.Net C #.

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection