正确实施带有回发的 Web 部件?
我想要做的是创建一个 Web 部件,其中有一个文本框,您可以在其中设置 Web 部件上的文字 (h2) 的值,以及一个回发的“保存”按钮,然后相应地设置文字。 这有一个巨大的警告: 当页面在回发后加载时,文字并没有改变。 但是,如果我记录文字中实际设置的内容,它就会具有新值。 此外,如果我再次重新加载页面(F5),它会正确显示。
起初我认为它一定是 ViewState,所以我对所有控件禁用了它。 我验证它没有保存在 ViewState 中(对其进行解码)。 所以 ViewState 不保存旧值。
我正在使用“CreateChildControls”将控件添加到 Web 部件。 回发由一个简单的事件处理程序处理。
有任何想法吗?
作为记录,我使用的是 MOSS 2007。
What I'm trying to do is to create a webpart that has a textbox where you can set the value of a literal (h2) on the webpart and a "save" button that posts back and then sets the literal accordingly. This works with one huge caveat; when the page loads after the postback the literal has not been changed. However if I log what is actually set in the literal it has the new value. Also if I reload the page again (F5) it displays correctly.
At first I figured it must be ViewState, so I disabled it for all controls. I verified that it is not being saved in the ViewState (decoded it). So ViewState is not saving the old value.
I'm using "CreateChildControls" to add my controls to the webpart. and the postback is handled by a simple event handler.
Any ideas?
For the record, I'm using MOSS 2007.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来像是 ASP.NET 事件计时问题。 尝试在页面加载事件中调用 EnsureChildControls() 。 这可确保在处理回发事件之前调用 CreateChildControls() 方法并将控件添加到页面。 如果您的控件首先在 PreRender 或 Render 阶段添加,那么它们获取回发数据就为时已晚。 然后,在下一页加载之前您将看不到更改。
Sounds like an ASP.NET event timing problem. Try calling EnsureChildControls() in the page load event. This ensures that your CreateChildControls() method is called and your controls are added to the page before the post back events are handled. If your controls are first added at the PreRender or Render stage it will be too late for them to pick up the post back data. You will then not see the change before the next page load.
也许这篇博文可以帮助您更好地理解 Web 部件的生命周期并解决您的问题。 http://platinumdogs.wordpress.com/2008/10 /14/sharepoint-webpart-lifecycle-events/
Perhaps this blog post might help you to understand the life cycle of a webpart better and to solve your problem. http://platinumdogs.wordpress.com/2008/10/14/sharepoint-webpart-lifecycle-events/
您始终可以使用 AJAX 更新面板,将文字控件放入其中,然后在更新面板上调用 UDP.Update。 另外,在您的初始示例中,检查您的文字控件上是否有
runat="server"
。 您应该能够向 Page_Load 事件添加更改,并且这应该显示在 Web 部件上。You could always use an AJAX update panel, drop your literal control inside that and call an UDP.Update on the update panel. Also with your initial example check you have
runat="server"
on your literal control. You should be able to add a change to the Page_Load event and this should appear on the webpart.这是一个建议。 无论如何,它对我有用。
Here is a suggestion. It works for me anyway.