为什么 __doPostBack() 不触发我的绑定加载事件?

发布于 2024-12-02 08:14:43 字数 1128 浏览 0 评论 0原文

情况是这样的:我的页面上有数量可变的动态创建的更新面板,所以我想我应该编写一种方法来处理每个更新面板的所有加载。

我的 Updatepanel 创建看起来像这样:

 Dim newUpdp As New UpdatePanel
 newUpdatep.ChildrenAsTriggers = False
 newUpdatep.UpdateMode = UpdatePanelUpdateMode.Conditional
 newUpdatep.ID = Guid.NewGuid.ToString

 AddHandler newUpdatep.Load, AddressOf updatep_load_method
 updatep_Holder.ContentTemplateContainer.Controls.Add(newUpdp)
 updatep_Holder.Update()  

这将创建更新面板并将其加载事件绑定到方法“updatep_load_method”。一旦 Updatepanel 插入支架,就会调用此方法。方法代码如下:

Private Sub updp_load_method(sender As UpdatePanel, e As System.EventArgs)
   Dim div As New HtmlGenericControl("div")
   div.InnerText = Date.Time.Now.ToString
   sender.ContentTemplateContainer.Controls.Add(div)
   sender.Update()
End Sub

稍后我要更新面板,刷新时间。所以我使用 javascript __doPostBack 方法。根据 Dave Ward 的说法,__doPostBack 方法遵循完整页面回发生命周期,所以我认为我的更新面板的加载事件将被触发,并且该特定更新面板将调用“updatep_load_method”...

尽管发生部分回发,并且调用其他更新面板的加载事件,但我的绑定一些不是。那么这里发生了什么?

Here's the situation: I have a variable number of dynamically created update panels on my page, so I thought I would write one method which handles all of the loading for each one.

My Updatepanel creation looks something like this:

 Dim newUpdp As New UpdatePanel
 newUpdatep.ChildrenAsTriggers = False
 newUpdatep.UpdateMode = UpdatePanelUpdateMode.Conditional
 newUpdatep.ID = Guid.NewGuid.ToString

 AddHandler newUpdatep.Load, AddressOf updatep_load_method
 updatep_Holder.ContentTemplateContainer.Controls.Add(newUpdp)
 updatep_Holder.Update()  

This creates the update panel and binds its load event to the method "updatep_load_method". This method is called as soon as the Updatepanel is inserted into the holder. The method code is as follows:

Private Sub updp_load_method(sender As UpdatePanel, e As System.EventArgs)
   Dim div As New HtmlGenericControl("div")
   div.InnerText = Date.Time.Now.ToString
   sender.ContentTemplateContainer.Controls.Add(div)
   sender.Update()
End Sub

A little later on I want to update the panel, and refresh the time. So I use the javascript __doPostBack method. According to Dave Ward, the __doPostBack method follows the full page postback lifecycle, so I figured the load event of my Update panel would be fired and that "updatep_load_method" would be called by that particular update panel...

Although the partial postback occurs, and other update panel's load events are called, my bound ones aren't. So what's happening here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最好是你 2024-12-09 08:14:43

您需要在 Page_Init 期间重新创建动态控件,并且必须使用相同的 Id(因此您不能在以下情况下执行 Guid.NewGuid.ToString()重新创建控件)。

否则,ASP.Net 将无法找到您的控件并用 ViewState 中的数据填充它。

这是一篇很好的文章< /a> 解释了有关动态控件的更多信息。

You need to recreate dynamic controls during Page_Init and have to use the same Id (so you cannot do a Guid.NewGuid.ToString() while recreating the control).

Otherwise, ASP.Net will not be able to locate your control and populate it with data from ViewState.

Here's a good article that explains more about dynamic controls.

追星践月 2024-12-09 08:14:43

尝试在代码中实现 RaisePostBackEvent 方法:

protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
    //call the RaisePostBack event 
    base.RaisePostBackEvent(source, eventArgument);

   //add whatever code you need to execute on postback here
}

Try implementing the RaisePostBackEvent method in your code:

protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
    //call the RaisePostBack event 
    base.RaisePostBackEvent(source, eventArgument);

   //add whatever code you need to execute on postback here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文