为什么 __doPostBack() 不触发我的绑定加载事件?
情况是这样的:我的页面上有数量可变的动态创建的更新面板,所以我想我应该编写一种方法来处理每个更新面板的所有加载。
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在
Page_Init
期间重新创建动态控件,并且必须使用相同的Id
(因此您不能在以下情况下执行Guid.NewGuid.ToString()
重新创建控件)。否则,ASP.Net 将无法找到您的控件并用
ViewState
中的数据填充它。这是一篇很好的文章< /a> 解释了有关动态控件的更多信息。
You need to recreate dynamic controls during
Page_Init
and have to use the sameId
(so you cannot do aGuid.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.
尝试在代码中实现 RaisePostBackEvent 方法:
Try implementing the RaisePostBackEvent method in your code: