I am having a strange problem with a custom web control in an ASP.NET forms application.
For a web application, I decided that it would save me time to create a class that inherits from CompositeControl , which combines the label with some other control, such as TextBox or DropDownList . This composite control has several properties that wrap properties of basic properties (for example: EnteredText wraps TextBox.Text ). Controls can be used to bind data in the same way as regular ones, but instead of txtBox.Text = someObject.someProperty , compositeControl.EnteredText = someObject.someProperty . These controls are then used on ASPX pages and custom ASCX controls.
The first version of web controls is written in C # and works great. These controls are located in the App_Code folder the website. However, this folder was getting quite large, and because of the policy I had to write everything in VB.NET, so I decided to create a class library to put the version of VB.NET controls in it so that I don’t have to translate everything Web applications on VB.NET, but only using controls.
However, the version of VB.NET does not work. When I click the link in the GridView , it opens a popup window and correctly fills all the text fields. When you close the screen and click on any other link from the same grid, the fields are not filled in properly and the data from the first postback is displayed in the fields. When using the debugger, I see that all the fields are filled with the corresponding data, and during the rendering phase, the Text property of the base TextBox shows all the new data. He just doesn't show it on the screen. Receiving data after postback, for example, when you click the save button, works as usual.
When I put the control from the App_Code folder on the same page, it works as expected and shows the new data, not the data from the previous postback.
Here are two bin insertion URLs, the first to the working version of C #, 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 of how it is 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 file:
<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, what makes the C # version work and the VB version not?
Thanks in advance! Feel free to ask questions if you want to clarify.
Update: I disabled ViewState on spanning ASCX, 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 trip round trip, each generation of dynamically created controls will get property values from the state view of the previous set of controls. In many cases, you can avoid this problem by setting the EnableViewState property of the container control to false. In this case, information about dynamic controls is not saved and there is no conflict with successive versions of controls.
I just don't know how this suddenly applies to controls. Based on this MSDN article on ViewState, I assume this is because controls are added to the CreateChildControls method that occurs after postback events.
Can someone explain what exactly is going on here?