Sharepoint Web 部件 OnPreRender 回发动态文本框在按钮处理程序中不可访问
我有一个自定义 Web 部件,它扩展 System.Web.UI.WebControls.WebPart 并实现 EditorPart。
我将所有静态控件添加到重写的 CreateChildControls 方法中,因为我知道这使得它们在回发中持久存在。
但是,我在重写的 OnPreRender 方法中添加了一些 TextBox,因为我添加的实际 TextBox 取决于我在 OnPreRender 中调用的 Web 服务返回的数据。必须在 OnPreRender 中调用 Web 服务,因为我需要在 EditorPart 中设置的一些属性值。如果我将此逻辑构建到 CreateChildControls 方法中,显然数据在应用编辑后的第一个 PostBack 上不可用,因为 PostBack 事件在 CreateChildControls 之后恢复。这意味着在刷新数据之前必须发布页面两次,这很混乱。
如何使这些文本框及其输入的文本值在回发中保持不变,以便我可以在按钮处理程序中访问它们?
感谢您的帮助, 马特
I have a custom webpart which extends System.Web.UI.WebControls.WebPart and implements an EditorPart.
I have all the static controls being added in the overridden CreateChildControls method as I know this makes them persistent across PostBacks.
However, I have some TextBoxes being added in the overridden OnPreRender method because the actual TextBoxes that I add depend upon the data returned from a Web Service which I am calling in OnPreRender. The Web Service has to be called in OnPreRender because I need some of the Property values that are set in the EditorPart. If I build this logic into the CreateChildControls method, obviously the data is not available on first PostBack after an edit is applied because the PostBack events are restored after CreateChildControls. This means the page has to be posted twice before the data is refreshed and that is just cludgy.
How can make these TextBoxes along with their entered text values persistent across PostBacks so that I can access them in the button handler?
Thanks for any help,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这最终对我有用:
由于“隐藏”TextBox 是在 CreateChildControls 中添加的,因此它的值会被恢复并可以在 PostBack 上访问。
希望这对那里的人有帮助,我知道我在这件事上花了一些时间。
This ended up working for me:
Because the "hidden" TextBox is added in CreateChildControls, its value is restored and can be accessed on PostBack.
Hopefully this helps someone out there, I know I burned some time on this one.
FWIW,您可以直接在 ViewState 中存储值,而无需通过 TextBox 控件内的代理访问它。在此代码片段中,我将对视图状态的访问包装在一个普通的旧属性中:
FWIW, You can store a values directly in the ViewState without having to access it via proxy inside a TextBox control. In this snippet, I wrapped access to the viewstate inside a plain old property: