ASP.NET Web 控件在第一次回发后显示旧数据

发布于 2024-11-29 15:15:57 字数 2696 浏览 0 评论 0原文

我在 ASP.NET 表单应用程序中的自定义 Web 控件方面遇到了一个奇怪的问题。

对于 Web 应用程序,我认为创建一个继承自 CompositeControl 的类会节省大量时间,该类将标签与其他一些控件(例如 TextBox)组合在一起或DropDownList。该复合控件具有多个包装底层属性的属性(例如:EnteredText 包装 TextBox.Text)。这些控件可用于数据绑定,其方式与常规控件相同,但您使用的是 compositeControl.EnteredText = someObject.someProperty,而不是 txtBox.Text = someObject.someProperty。 这些控件随后用于 ASPX 页面和 ASCX 用户控件。

Web 控件的第一个版本是用 C# 编写的,并且运行良好。这些控件放置在网站的 App_Code 文件夹 中。然而,这个文件夹变得相当大,并且由于政策,我必须在 VB.NET 中编写所有内容,因此我决定创建一个类库,将 VB.NET 版本的控件放在那里,这样我就不用将整个 Web 应用程序转换为 VB.NET,但仅转换控件。

但是,VB.NET 版本无法运行。当我单击 GridView 中的链接时,它会打开一个弹出窗口并正确填充所有文本框。当关闭屏幕并单击同一网格中的其他链接时,这些框不会正确填充,并且来自第一次回发的数据在框中可见。使用调试器时,我看到所有框都填充了正确的数据,并且在渲染阶段,底层 TextBox 的 Text 属性显示了所有新数据。它只是没有在屏幕上显示出来。回发后检索数据(就像按下保存按钮一样)可以正常工作。

当我将 App_Code 文件夹中的控件放置在同一页面上时,它会按预期工作并显示新数据,而不是先前回发的数据。

这里有两个粘贴 bin URL,第一个指向工作 C# 版本,第二个指向 VB.NET 版本。

C# 版本:http://pastebin.com/SuWaHrMt

VB.NET 版本:http://pastebin.com/9aeV7St1

以下是如何在 ASCX 文件中使用它的示例

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomControl.ascx.cs" Inherits="Controls_CustomControl" %>
<%@ Register Namespace="UI.CompositeControls" TagPrefix="cs" %>

<cs:TextBoxWithLabel ID="TextBoxWithLabel1" runat="server" LabelText="Voornaam" />
<cc:LabeledTextBox ID="tbwlVoornaam" runat="server" LabelText="Voornaam" />

: cc 前缀在 web.config 中指定:

<pages theme="Default" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
  <controls>
    <add assembly="CompositeControls" namespace="CompositeControls" tagPrefix="cc" />
  </controls>
  <namespaces>
    <add namespace="CompositeControls" />
  </namespaces>
</pages>

我在这里缺少什么,使得 C# 版本工作而 VB 版本不工作?

提前致谢!如果您需要澄清,请随时提问。

更新: 我禁用了相关 ASCX 上的 ViewState,它起作用了:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomControl.ascx.cs" EnableViewState="false" Inherits="Controls_CustomControl" %>

我的同事发现了以下片段:

如果您在每次往返时重新插入控件,则每一代 动态创建的控件将从视图中获取属性值 前一组控件的状态。在许多情况下,您可以避免这种情况 通过将容器控件的 EnableViewState 属性设置为 错误的。在这种情况下,不会保存有关动态控件的信息,并且 与后续版本的控件没有冲突。

我只是不确定这如何突然适用于控件。基于 这篇关于 ViewState 的 MSDN 文章 我猜测发生这种情况是因为控件添加到回发事件之后发生的 CreateChildControls 方法中。

有人可以解释一下这里到底发生了什么吗?

I'm having a weird problem with a custom Web Control in an ASP.NET forms application.

For a web application, I figured that it would save me a lot of time to create a class which inherits from CompositeControl which combines a label with some other control such as a TextBox or DropDownList. This composite control has several properties that wrap properties of the underlying properties (e.g.: EnteredText wraps TextBox.Text). The controls can be used for databinding in the same way as the regular ones, but instead of txtBox.Text = someObject.someProperty you use compositeControl.EnteredText = someObject.someProperty.
These controls are then used on ASPX pages and ASCX user controls.

The first version of the web controls is written in C# and works perfectly. These controls are placed in the App_Code folder of the website. However, this folder was getting rather big and because of policy I have to write everything in VB.NET, so I decided to make a class library to put the VB.NET version of the controls in there, so that I don't have to translate the entire web application to VB.NET but only the controls.

However, the VB.NET version does not work. When I click the link in a GridView, it opens a popup and populates all the textboxes properly. When close the screen and click some other link from the same grid, the boxes are not populated properly and the data from the first postback is visible in the boxes. When using the debugger, I see that all boxes are populated with the proper data, and during the render phase the Text property of the underlying TextBox shows all of the new data. It just doesn't show that on screen. Retrieving the data after postback, like when pressing a save button, works as normal.

When I place a control from the App_Code folder on the same page, it works as expected and shows the new data and not the data from the previous postback.

Here are two paste bin URLs, the first goes to the working C# version and the second to the VB.NET version.

C# version: http://pastebin.com/SuWaHrMt

VB.NET version: http://pastebin.com/9aeV7St1

Here is an example how it's used in an ASCX file:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomControl.ascx.cs" Inherits="Controls_CustomControl" %>
<%@ Register Namespace="UI.CompositeControls" TagPrefix="cs" %>

<cs:TextBoxWithLabel ID="TextBoxWithLabel1" runat="server" LabelText="Voornaam" />
<cc:LabeledTextBox ID="tbwlVoornaam" runat="server" LabelText="Voornaam" />

The cc prefix is specified in the web.config:

<pages theme="Default" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
  <controls>
    <add assembly="CompositeControls" namespace="CompositeControls" tagPrefix="cc" />
  </controls>
  <namespaces>
    <add namespace="CompositeControls" />
  </namespaces>
</pages>

What am I missing here that makes the C# version work and the VB version not?

Thanks in advance! Feel free to ask questions if you want some clarification.

Update:
I disabled the ViewState on the encompassing ASCX in question and it works:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomControl.ascx.cs" EnableViewState="false" Inherits="Controls_CustomControl" %>

My colleague found the following snippet:

If you reinsert controls with each round trip, each generation of
dynamically created controls will pick up property values from the view
state of the preceding set of controls. In many cases, you can avoid this
problem by setting the EnableViewState property of the container control to
false. In that case, no information about the dynamic controls is saved, and
there is no conflict with successive versions of the controls.

I'm just not sure how this suddenly applies to the controls. Based on this MSDN article on ViewState I'm guessing that it happens because the Controls are added in the CreateChildControls method which occurs after the postback events.

Can someone explain what exactly is going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

素衣风尘叹 2024-12-06 15:15:57

您在哪些浏览器中遇到了该问题?
我最近在 Firefox 中遇到了类似的问题,只需禁用 Firebug 缓存即可解决。
(https://i.sstatic.net/bRRjb.jpg)

In which browsers are you experiencing the issue?
I recently had a similar problem in Firefox that was solved by just disabling Firebug caching.
(https://i.sstatic.net/bRRjb.jpg)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文