So I am not able to call an event on an update panel after the first postback. The event is assigned to the control on postback, but doesn't work. The code below is what happens in the control added after postback.
//Add Delete Control
up.ContentTemplateContainer.Controls.Add(new LiteralControl("<div class=\"news_delete\">"));
LinkButton deleteLink = new LinkButton();
deleteLink.ID = item.ID.ToString();
deleteLink.Text = "Delete This News Item";
deleteLink.Click += new EventHandler(DeleteNewsItem);
up.ContentTemplateContainer.Controls.Add(deleteLink);
up.ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));
The control gets added to the update panel and I can see it, but when I click on the link button it doesn't run the event DeleteNewsItem. The event can be seen below.
//OnSubmits
void DeleteNewsItem(object sender, EventArgs e)
{
SPList newsList;
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
web.Update();
newsList = web.Lists["News Wire"];
SPListItemCollection items;
items = newsList.Items;
LinkButton item2Delete = (LinkButton)sender;
SPListItem item = items.GetItemById(Int32.Parse(item2Delete.ID));
item.Delete();
newsList.Update();
web.AllowUnsafeUpdates = false;
System.Web.HttpContext.Current.Response.Redirect(SPContext.Current.Web.Url);
}
So once again i would like reiterate that it works before I do a postback on the update panel. What I mean is the first set of items that have this delete control work and delete the list item, but when I do the asynchronous postback it doesn't ever run the event when I click on it. I have set breakpoints and it never reaches it(the event). I wonder if this has to do with the web part lifecycle or the page lifecycle. Please someone help. By the way this is not a visual webpart, but it is a webpart.
This is indeed a lifecycle issue. Take a look at the following post, which will explain your problem and a possible workaround : ASP.Net Dynamically populate controls on postback
The following post can also give a workaround for the problem.