Click here to Skip to main content
14,979,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on application which requires to generate dynamic columns in gridview and then bound with linkbutton.
this much is done , but i have to add click event to link button but event is not fired .
below is code which i have tried.
below code is inside rowbound event of gridview.

What I have tried:

foreach (DateTime dt in days)
LinkButton lb = new LinkButton();

lb.ID = "lb" + i;
lb.Attributes.Add("runat", "server");
lb.Text = r1[dt.Date.ToString("dd-MM-yyyy")].ToString();
lb.CommandName = "UpDate";
lb.Command += lb_Command;
// lb.OnClientClick += new EventHandler(onLinkClick);
// lb.Click += new System.EventHandler(onLinkClick);
// lb.DataBinding += lb_DataBinding;

row.Cells[i + 2].Controls.Add(lb);
if (dt.Date <= System.DateTime.Today)
lb.Enabled = false;
Updated 21-Jun-16 7:37am
F-ES Sitecore 21-Jun-16 5:50am
Does that code run on post back also?
Member 11360268 21-Jun-16 5:53am
didnt get u , it run during gridview binding
F-ES Sitecore 21-Jun-16 5:56am
Does the binding happen when you do a page postback though, or do you only bind when it isn't a postback? You have to re-create your links and re-attach the events on postback or else they don't fire.
Member 11360268 21-Jun-16 8:18am
ohk, it is presently not binding on postback .i ll try binding on postback

1 solution

You may need to create the control at RowCreated event instead. Here's a working code for your reference:

<asp:content id="Content2" contentplaceholderid="MainContent" runat="server" xmlns:asp="#unknown">
    <asp:gridview id="GridView1" runat="server" onrowcreated="GridView1_RowCreated">
                        <asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>


using System;
using System.Web.UI.WebControls;
using System.Data;

namespace WebFormDemo
    public partial class DynamicControlInGridView : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack)

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow) {

                LinkButton lb = new LinkButton();
                lb.ID = "LinkButton1";
                lb.Text = "Click Me!";
                lb.Click += OnLinkClick;

                PlaceHolder p = (PlaceHolder)e.Row.FindControl("PlaceHolder1");


        protected void OnLinkClick(object sender, EventArgs e) {
            LinkButton lb = (LinkButton)sender;
            GridViewRow row = (GridViewRow)lb.NamingContainer;
            if (row != null) {
                Response.Write("Found it!");

        private void BindGridView() {
            GridView1.DataSource = CreateDataSource();

        public DataTable CreateDataSource() {
            DataTable dt = new DataTable();
            DataRow dr;

            dt.Columns.Add(new DataColumn("ID", typeof(string)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Lastname", typeof(string)));

            dr = dt.NewRow();
            //add values to each columns
            dr["ID"] = 1;
            dr["Name"] = "Vincent";
            dr["LastName"] = "Durano";
            return dt;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900