title: Asp.net:to call event handler of dynamically generated buttons?
body: <p>i have set enableviewstate property of linkbutton and label to true.also regenrating same buttons on postback in pageload event handler.but m not able to call onclick event handler of linkbutton.can u please tell me what is problem with code?</p>
  
  <p>public partial class _Default : System.Web.UI.Page 
  {
      protected void Page_Load(object sender, EventArgs e)
      {</p>
  
  <pre><code>        List&lt;LinkButton&gt; listOfLinkButton = Session["ListOfLinkButton"] as List&lt;LinkButton&gt;;
          List&lt;Label&gt; listOfLabel = Session["ListOfLabel"] as List&lt;Label&gt;;
          if(listOfLabel!=null &amp;&amp; listOfLinkButton!=null)
          {
              for (int i = 0; i &lt; listOfLinkButton.Count; i++)
              {
                  PlaceHolder1.Controls.Add(listOfLinkButton[i]);
                  PlaceHolder1.Controls.Add(new LiteralControl("&lt;br /&gt;"));
                  PlaceHolder1.Controls.Add(listOfLabel[i]);
                  PlaceHolder1.Controls.Add(new LiteralControl("&lt;br /&gt;"));
              }
          }
  
  
  
  }
  
  
  
  protected void LinkButton_Click(object sender, EventArgs e)
  {
      LinkButton linkButton = sender as LinkButton;
  
      Response.Redirect(linkButton.Attributes["LinkUrl"]);        
  }
  protected void Button1_Click1(object sender, EventArgs e)
  {
  
      List&lt;LinkButton&gt; listOfLinkButton = new List&lt;LinkButton&gt;();
      List&lt;Label&gt; listOfLabel = new List&lt;Label&gt;();
      Rss rssDocumentObj = RssFileReader.GetRssDocumentData(TextBox1.Text);
      for (int j = 0; j &lt; rssDocumentObj.ListOfChannel.Count; j++)
      {
          LinkButton linkButton = new LinkButton();
          linkButton.ID = "LinkButtonForChannelDynamicInPlaceHolder1Id" + j;
          linkButton.EnableViewState = true;
          linkButton.ForeColor = Color.Blue;
          linkButton.Font.Bold = true;
          linkButton.Font.Size = 18;
          linkButton.Font.Underline = true;
          linkButton.Text = rssDocumentObj.ListOfChannel[j].ChannelTitle.InnerText;
          linkButton.Click += new EventHandler(LinkButton_Click);
          linkButton.Attributes.Add("LinkUrl", rssDocumentObj.ListOfChannel[j].ChannelLink.InnerText);
          linkButton.Attributes.Add("onmouseover", "this.style.color = '#006699'");
          linkButton.Attributes.Add("onmouseout", "this.style.color = '#0000ff'");
          PlaceHolder1.Controls.Add(linkButton);
          listOfLinkButton.Add(linkButton);
          PlaceHolder1.Controls.Add(new LiteralControl("&lt;br /&gt;"));
          Label label = new Label();
          label.ID = "LabelForChannelDynamicInPlaceHolder1Id" + j;
          label.EnableViewState = true;
          label.ForeColor = Color.DarkSlateGray;
          label.Text = rssDocumentObj.ListOfChannel[j].ChannelDescription.InnerText;
          PlaceHolder1.Controls.Add(label);
          listOfLabel.Add(label);
          PlaceHolder1.Controls.Add(new LiteralControl("&lt;br /&gt;"));
  
          for (int i = 0; i &lt; rssDocumentObj.ListOfChannel[j].ListOfItem.Count; i++)
          {
              LinkButton linkButtonForItem = new LinkButton();
              linkButtonForItem.ID = "LinkButtonDynamicInPlaceHolder1Id" + j + " " + i;
              linkButtonForItem.EnableViewState = true;
              linkButtonForItem.ForeColor = Color.Blue;
              linkButtonForItem.Font.Bold = true;
              linkButtonForItem.Font.Size = 14;
              linkButtonForItem.Font.Underline = false;
              linkButtonForItem.Text = rssDocumentObj.ListOfChannel[j].ListOfItem[i].ItemTitle.InnerText;
              linkButtonForItem.Click += new EventHandler(LinkButton_Click);
              linkButtonForItem.Attributes.Add("LinkUrl", rssDocumentObj.ListOfChannel[j].ListOfItem[i].ItemLink.InnerText);
              linkButtonForItem.Attributes.Add("onmouseover", "this.style.color = '#006699'");
              linkButtonForItem.Attributes.Add("onmouseout", "this.style.color = '#0000ff'");
              PlaceHolder1.Controls.Add(linkButtonForItem);
              listOfLinkButton.Add(linkButtonForItem);
              PlaceHolder1.Controls.Add(new LiteralControl("&lt;br /&gt;"));
              Label labelForItem = new Label();
              labelForItem.ID = "LabelDynamicInPlaceHolder1Id" + i;
              labelForItem.EnableViewState = true;
              labelForItem.ForeColor = Color.DarkGray;
              labelForItem.Text = rssDocumentObj.ListOfChannel[j].ListOfItem[i].ItemDescription.InnerText;
              PlaceHolder1.Controls.Add(labelForItem);
              listOfLabel.Add(labelForItem);
              PlaceHolder1.Controls.Add(new LiteralControl("&lt;br /&gt;"));
              Session["ListOfLinkButton"] = listOfLinkButton;
              Session["ListOfLabel"] = listOfLabel;
          }
      }  
  }
  </code></pre>
  
  <p>}</p>