1

好的,

所以我创建了这个 Web 应用程序,它被设计成一个单页应用程序。有一个菜单仅在单个更新面板中加载各种用户控件(设计为表单)中的一个(清除控件、加载新控件等)。用户执行任何输入、单击保存、删除等操作。在这些用户控件中,有几个数据绑定和内部依赖控件,这是我将它们加载到更新面板中的主要原因。

现在,我遇到了这个问题,当用户选择一个新菜单,然后去更改数据绑定控件的值时,该控件仅在第一次回发时丢失其值。我很确定这是由于清除了用户控件所在的容器,因为我尝试清除和加载常规面板,但我会遇到完全相同的问题。由于在我的情况下使用 (!IsPostBack) 不起作用,我只是创建了一个布尔会话变量来在数据绑定方面在页面和用户控件之间执行相同的逻辑。为了进一步测试我的问题,我将应用程序更改为多页应用程序,问题就消失了;值在回发时保留。

因此,要达到我的初衷,因为我看到了使用多个页面的功能;我认为在单页版本中,如果我只是将“Server.Transfer(thedefaultpage.aspx)”添加到代码中,我将保留从多页方法中获得的功能,同时让用户保持在同一页面上。它工作得很好。不会丢失数据,它可以正常工作。这是不是味道很差?所有用户功能和职责总是在一个页面上执行,导航对他们没有任何作用。

我只是担心从本质上刷新页面只是不好的做法或 hacky,但它适用于应用程序的目的,并且在可能不需要时不必创建 2-3 打页面。我已经阅读了一些线程,其中有人会这样做只是为了清除结果或一些小问题,但仅此而已。

如果这是不好的做法(或者即使不是),也许有人可以解决第一次回发价值损失的问题吗?我在主控、页面、用户控件和数据绑定的子控件上启用了 enableviewstate=true 和 viewstatemode=enabled。提前感谢您的任何意见。

4

3 回答 3

0

这种做法会在未来导致很多问题。我不会这样

于 2013-08-25T05:17:57.957 回答
0

是的,这是不好的做法。WebForms 的状态问题随着 UI 复杂性的增加而逐渐恶化。

我使用一个序列化为 XML 并作为纯文本保存到数据库中的单个类来持久化 SPA UI 状态。我在 URL 中使用 GUID 作为会话密钥(但您可以使用用户 ID、ASP.NET 会话 ID 或其他任何内容)来检索每个请求的状态信息,从而消除管理状态或丢失数据时出现的任何问题用户刷新页面。

这样做还有一个额外的好处,就是允许您使用单元测试覆盖逻辑 UI,而不会纠结于 Web 层的细节——这对于复杂的工作流程很有用。

但是,如果您尝试这样做,我建议您考虑迁移到 MVC 并使用普通的旧 Post-and-Redirect;当您想要允许后退按钮时,将哈希标签添加到重定向响应中。

于 2013-11-30T18:26:08.717 回答
0

So, in the event anyone comes across this same issue and is without a legitimate workaround, I "came up" with one the other day when I came back to solving this issue.

The first postback issue seems to stem from the clearing and repopulation of a container control (placeholder, panel, updatepanel, etc.). As I had the user click on a menu item that would then clear and load usercontrols into the container, the usercontrol being referenced on postback differed from the new one being loaded. Bear with me on how I will attempt to describe the situation...

The last loaded usercontrol is referenced on the initial menu click, before the menu click event fires and then, the container is cleared and the new usercontrol is loaded from the menu click event. This does not happen when there are no controls loaded in the container (initial startup). As the usercontrols are added dynamically, I have a method that reloads the currently loaded usercontrol, it is placed in Page_Init. This event, along with PreInit, _Load, all fire before control events, and was the cause of my problem. The fix is, as will be obvious with more experience, is to add an if statement to both the usercontrol load method and Page_Init (or wherever you have the load method) that determines what is causing the postback. As, I only want the user to click on menu items to move within the numerous tasks, I just made sure that if the menu item click is causing the postback, then, do not run the load method in Init, but, anything else that causes postback (for me, only within the user control), the load method within Init will only fire then.

Additionally, the if statement is necessary within the load method, to make sure all of controls within the newly added usercontrols contain default/empty values, essentially whatever defaults you want the user to see when the control is first displayed, in my case I had:

if (Page.Request.Params["_EVENTTARGET"].Contains("Menu1") == true) 
{ 
uc.Clear(null, null);
uc.SetBoundControls();
}

)

Hopefully this helps someone else, or sparks a thought towards a better solution.

于 2013-09-10T22:16:13.713 回答