Sharepoint Web 部件 OnPreRender 回发动态文本框在按钮处理程序中不可访问

发布于 2024-09-01 07:59:21 字数 497 浏览 3 评论 0原文

我有一个自定义 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 技术交流群。

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

发布评论

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

评论(2

々眼睛长脚气 2024-09-08 07:59:21

这最终对我有用:

  • 在 CreateChildControls 中添加一个“隐藏”文本框(我通过 width=0 实现了这一点;visible=false 和enabled=false 不起作用)
  • 在 PreRender 中,检查在动态 TextBox(es) 中输入的任何值为了将参数传递给数据调用(在我的例子中是 Web 服务),这将来自解析“隐藏”文本框中的值(稍后解释)。
  • 数据调用后,添加文本框,确保重新填充上次回发之前输入的任何先前值(通过解析“隐藏”文本框文本)
  • 最后,向名为“onClick”的按钮添加一个属性,并将值设置为获取文本框值的 javascript 函数(使用 document.getElementById 并为每个文本框传递 TextBox.ClientID)并创建一个格式化字符串以将 TextBox ID 和值存储在“隐藏”TextBox 中。

由于“隐藏”TextBox 是在 CreateChildControls 中添加的,因此它的值会被恢复并可以在 PostBack 上访问。

希望这对那里的人有帮助,我知道我在这件事上花了一些时间。

This ended up working for me:

  • Add a "hidden" TextBox in CreateChildControls (I achieved this with width=0; visible=false and enabled=false don't work)
  • In PreRender, check for any values entered in dynamic TextBox(es) for passing params to data call (Web Service in my case), this will come from parsing the value in the "hidden" TextBox (explained later)
  • After data call, add TextBox(es) ensuring to repopulate any previous values entered before last postback (by parsing "hidden" TextBox Text)
  • Finally, add an attribute to the button named "onClick" and set the value to a javascript function that acquires the values of the TextBoxes (using document.getElementById and passing TextBox.ClientID for each TextBox) and creates a formatted string to store the TextBox ID and value in the "hidden" TextBox.

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.

忱杏 2024-09-08 07:59:21

FWIW,您可以直接在 ViewState 中存储值,而无需通过 TextBox 控件内的代理访问它。在此代码片段中,我将对视图状态的访问包装在一个普通的旧属性中:

public string WebServiceResult
{
  get
  {
    string text = (string) ViewState["WebServiceResult"];
    if (text != null)
       return text;
    else
       return string.Empty;
  }
  set
  {
    ViewState["WebServiceResult"] = value;
  }
}

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:

public string WebServiceResult
{
  get
  {
    string text = (string) ViewState["WebServiceResult"];
    if (text != null)
       return text;
    else
       return string.Empty;
  }
  set
  {
    ViewState["WebServiceResult"] = value;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文