Question : ASP.NET C# User Control Issue

I have created an ASP.NET user control and I am dynamically creating some ASP.NET controls in a Render override method and adding a click event. But if I click the control the page posts back but the event does not fire. If I create the same control with the same code in another area (eg. Page_Load) it works fine. What am I doing wrong?

I have attached some sample code below, but have removed the non-relevant parts of the code.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_Pager2 : System.Web.UI.UserControl
{
	protected void Page_Load(object sender, EventArgs e)
	{
	}

	protected override void Render(HtmlTextWriter writer)
	{
		LinkButton linkButton = new LinkButton();
		linkButton.ID = "btnNext";
		linkButton.Text = "Next";
		linkButton.CommandArgument = (_pageNumber++).ToString();
		linkButton.Click += btnNext_Click;
		PlaceHolder1.Controls.Add(linkButton);
		base.Render(writer);
	}

	protected void btnNext_Click(object sender, EventArgs e)
	{
		Response.Write("Clicked");
	}

}

Answer : ASP.NET C# User Control Issue

Random Solutions  
 
programming4us programming4us