gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:51:26 25 4
gpt4 key购买 nike

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

对于 Web 应用程序,我认为创建一个继承自 CompositeControl 的类会节省我很多时间,该类将标签与一些其他控件(例如 TextBoxDropDownList。这个复合控件有几个包装底层属性的属性(例如:EnteredText 包装 TextBox.Text)。这些控件可以像常规控件一样用于数据绑定(bind),但您使用的不是 txtBox.Text = someObject.someProperty compositeControl.EnteredText = 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" %>

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

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.

我只是不确定这如何突然适用于控件。基于this MSDN article on ViewState我猜这是因为在回发事件之后发生的 CreateChildControls 方法中添加了控件。

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

最佳答案

您在哪些浏览器中遇到了问题?我最近在 Firefox 中遇到了类似的问题,通过禁用 Firebug 缓存解决了这个问题。(http://i.stack.imgur.com/bRRjb.jpg)

关于c# - ASP.NET Web 控件在第一次回发后显示旧数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7083470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com