正确地实现带有回发的Webpart? [英] Properly implement a webpart with postback?

查看:90
本文介绍了正确地实现带有回发的Webpart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是创建一个具有文本框的Web部件,您可以在其中设置WebPart上的文字(h2)值,并使用保存"按钮回发,然后相应地设置文字.这需要一个巨大的警告.当页面在回发后加载时,文字未更改.但是,如果我记录实际在文字中设置的内容,它将具有新值.另外,如果我再次重新加载页面(F5),则它会正确显示.

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.

起初,我认为它必须是ViewState,所以我为所有控件禁用了它.我确认它没有保存在ViewState中(已解码).因此,ViewState不会保存旧值.

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.

我正在使用"CreateChildControls"将控件添加到Webpart.回发由一个简单的事件处理程序处理.

I'm using "CreateChildControls" to add my controls to the webpart. and the postback is handled by a simple event handler.

有什么想法吗?

出于记录,我使用的是MOSS 2007.

For the record, I'm using MOSS 2007.

推荐答案

听起来像ASP.NET事件计时问题.尝试在页面加载事件中调用SecureChildControls().这样可以确保在处理回发事件之前,调用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.

这篇关于正确地实现带有回发的Webpart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆