title: Dynamically added gridview's linkbutton not working
body: <p>I have a gridview that has dynamically created columns.</p>
  
  <p>Right now all the data is showing in its respective place but I am unable to get my link button to work. (Gridview disappears)</p>
  
  <p>Here is my backend code, the gridview used is just an empty gridview</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!this.IsPostBack)
      {
          TemplateField tfield = new TemplateField();
          tfield.HeaderText = "View";
          GridView1.Columns.Add(tfield);
      }
      GridView1.DataBind();
  }
  
  string selectedID = null;
  private void BindGrid(List&lt;string&gt; SelectedInfo)
  {
      DataTable dt = new DataTable();
  
      //Dynamically adding columns and setting first column added as ID
      selectedID = SelectedInfo[0];
      for (int i = 0; i &lt; SelectedInfo.Count; i++)
      {
          dt.Columns.Add(new DataColumn(SelectedInfo[i], typeof(string)));
      }
  
      List&lt;string[]&gt; InfoList = getInfoList(SelectedInfo);
  
      for (int i = 0; i &lt; InfoList.Count; i++)
      {
          dt.Rows.Add(InfoList[i]);
      }
  
      GridView1.DataSource = dt;
      GridView1.DataBind();
  }
  
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {   //Adding link button to first column
          LinkButton lnkView = new LinkButton();
          lnkView.ID = "lnkView";
          lnkView.Text = "View";
          lnkView.Click += ViewDetails;
          lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row[selectedID].ToString();
          e.Row.Cells[0].Controls.Add(lnkView);
      }
  }
  
  protected void ViewDetails(object sender, EventArgs e)
  {
      //Popup the selected row ID
      LinkButton lnkView = (sender as LinkButton);
      GridViewRow row = (lnkView.NamingContainer as GridViewRow);
      string id = lnkView.CommandArgument;
      ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Row ID" + id + "')", true);
  }
  
  protected void CheckBoxList2_SelectedIndexChanged(object sender, EventArgs e)
  {
      List&lt;string&gt; SelectedInfo = new List&lt;string&gt;();
      for (int i = 0; i &lt; CheckBoxList2.Items.Count; i++)
      {
          if (CheckBoxList2.Items[i].Selected)
          {
              SelectedInfo.Add(CheckBoxList2.Items[i].Text);
          }
      }
  
      BindGrid(SelectedInfo);
  }
  </code></pre>