简体   繁体   English

第一次异步回调后,无法在添加到更新面板的新控件上调用事件处理程序

[英]Not able to call event handler on new control added to update panel after first asynchronous call back

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. 该控件被添加到更新面板中,并且可以看到它,但是当我单击链接按钮时,它不会运行事件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. 我想知道这是否与Web部件生命周期或页面生命周期有关。 Please someone help. 请有人帮忙。 By the way this is not a visual webpart, but it is a webpart. 顺便说一下,这不是可视化的Web部件,而是一个Web部件。

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 看看下面的文章,它将解释您的问题和可能的解决方法: ASP.Net在回发时动态填充控件

The following post can also give a workaround for the problem. 以下帖子也可以提供解决该问题的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将带有事件处理程序的控件动态添加到更新面板,以及如何通过AJAX将事件处理程序回发 - How to add a control with an event handler dynamically to an update panel and have the event handler post back via AJAX 在第二个控件位于第一个控件内的情况下,如何从一个控件调用事件处理程序到另一个控件? - How to call an event handler from one control to the another control where the second control is inside the first control? 调用.Update()后绘画到面板 - Painting to panel after .Update() call 回发后通过背后的代码添加到更新面板的项目不会触发事件 - Event doesn't fire for an item that is added to the update panel through code behind after post back 在C#中执行异步方法调用之后,设置事件处理程序(或延迟)是否安全? - is it safe to set an event handler (or delagate) AFTER an asynchronous method call in C#? 由于未知原因,第一次调用可等待的异步函数是同步的 - First call to await-able asynchronous function is synchronous for unknown reason 如何从一个控件调用事件处理程序到另一个控件? - How to call an event handler from one control to the another control? 回发后重新绑定控件后,更新面板中的事件未触发 - Event in Update panel is not fired after rebinding control after Postback 从宿主页面在用户控件中调用事件处理程序 - Call an event handler in a user control from host page 事件处理程序返回后,在事件处理程序中的WebForms控件上设置的属性会还原 - Property set on a WebForms control in an event handler reverts back after event handler returns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM