Create Link button dynamically in c#

Posted by in C# category on for Beginner level | Points: 250 | Views : 53370 red flag

How to create link button control in run time using C#

Create Link button dynamically in c#

In this article we are going to look into how to create dynamic control in C# application. In project sometimes we may come up with a requirement to create control in runtime. There might be situation where we want to create link button according to stored data in database. As we don’t know how much data has stored in database we cannot create control in design time. So we have to create control in run time.

Step 1)  Create aspx page like below.

Page Language="C#" AutoEventWireup="true" CodeBehind="LinkTest.aspx.cs" Inherits="ASP.NET.LinkTest"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br /><br />
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

This aspx page is very simple with two controls in design time. One label to display text and one placeholder control to keep all control which will create in runtime.

Step 2) Add below C# code in your .cs file 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ASP.NET
    public partial class LinkTest : System.Web.UI.Page
    private void CreateControl()
            Int32 i; //create a integer variable
            for(i = 0; i < 5; i++) // will generate 10 LinkButton
             //create instance of LinkButton
                LinkButton lb = new LinkButton();
                lb = new LinkButton();
                lb.Text = Convert.ToString(i) + " "; //LinkButton Text
                lb.ID = Convert.ToString(i); // LinkButton ID’s
                lb.CommandArgument = Convert.ToString(i); //LinkButton CommandArgument
                lb.CommandName = Convert.ToString(i); //LinkButton CommanName
                lb.Command += new CommandEventHandler(lb_Command);//Create Handler for it.
lb.Command += and press double time Tab Key it will generat the lb_Command()
                PlaceHolder1.Controls.Add(lb); // Adding the LinkButton in PlaceHolder
        void lb_Command(object sender, CommandEventArgs e)
            LinkButton lnk = sender as LinkButton;
            Label1.Text = e.CommandName; // will display the which Linkbutton clicked
            if(lnk.Font.Bold == true)
                lnk.Font.Bold = false;
                lnk.ForeColor = System.Drawing.Color.Blue;
                lnk.Font.Bold = true;
                lnk.ForeColor = System.Drawing.Color.Green;
        protected void Page_Load(object sender, EventArgs e)

In page load event , CreateControl() is called and in CreateControl() function for loop we are creating 5 link buttons. We are assigning button property within for loop.

And by below line


lb.Command += new CommandEventHandler(lb_Command);

we are initializing event handler for each and every link button. Now, If you go in event handler you will find code to make button text bold and normal after every click.

If you want to decorate each link button after every click simply you have to make change in event handeler code.

Conclusion :

This is simple example to create link button dynamically . You can create any control in same way

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)