对于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" %>
我的同事找到了以下代码段:
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 @R_97_4045@ion about the dynamic controls is saved,and
there is no conflict with successive versions of the controls.
我只是不确定这是如何突然适用于控件的.基于this MSDN article on ViewState我猜测它发生是因为控件是在回发事件之后发生的CreateChildControls方法中添加的.
有人能解释一下究竟发生了什么吗?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。