C# .NET - Programmatically add link button - Asked By srilatha janakaraj on 12-Dec-05 04:26 PM

This is what I got on my user control..


//programatically add a link button
LinkButton LB1 = new LinkButton();
LB1.CommandName = "Products";
LB1.CommandArgument = "something";
LB1.Command += new System.Web.UI.WebControls.CommandEventHandler(OnLinkClick);

void OnLinkClick(object O, System.Web.UI.WebControls.CommandEventArgs E)
//do something here.

The "Build_datagrid" function will be called from another web page.
The link button gets built fine. But when I click on link button LB1, it does not fire off the "OnLinkClick" function..

What am I missing here?

Should I add the CommandEventHandler code outside the "Build_datagrid" function?

Try this - Asked By Aarthi Saravanakumar on 12-Dec-05 04:39 PM

private void Page_Load(object sender, System.EventArgs e)
			LinkButton lnk = new LinkButton();
			lnk.Command+=new CommandEventHandler(lnk_Command);

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
			this.Load += new System.EventHandler(this.Page_Load);


		private void lnk_Command(object sender, CommandEventArgs e)

I dont want to do in page_load() - Asked By srilatha janakaraj on 12-Dec-05 04:44 PM

I want to add the link button only when some precondition is satisfied.. wjhich is done in function "Build_datagrid".. 

If I am adding this new linkbutton code to page_load, it would get fired for every page_load which is not what I want :-(

Should not make - Asked By Aarthi Saravanakumar on 12-Dec-05 04:50 PM

a difference.
Just check and make sure that the Link Button's event is handled in the UserControl.
Also if you are adding this as a button column in a DataGrid, then handle the OnItemCommand event,