Last post Jan 30, 2011 08:10 AM by Maxim Kornilov
Jan 29, 2011 11:54 AM|Maxim Kornilov|LINK
I am creating new custom Web Server control wich inherits form the CompositeControl.
I am encountered with the problem that Label loses viewstate during postbacks to server when it is used in the Composite Control (as child control). It seems that is problem only with the Label control, because TextBox works correctly. Setting EnableViewState
label property to "true" doesn't help to resolve problem.
To reproduce issue I created test composite control which includes Label, TextBox and Button as child controls. This test control sets text of label and textbox with hardcoded value when button is clicked. Then i created test aspx page which includes ScriptManager,
UpdatePanel with two instance of the created test control. And now i can see that if I click to the button of the first control then text are of the label and text box of the first control are changed as expected. Then I click to the button of the second control,
the label and text box of the second control are changed as expected and the label of the first control loses it value (in the same time TextBox displays correct value).
Also, when I use common User Control instead of Composite control it seems that everything is OK.
I perfomed google search, but currectly didn't find the solution to this problem. The most articles that can be related to this problem are:
Anybody know the solution to this problem? Is it really a well know issue?
To clarify what i mean there is code that I used to reproduce this issue
Custom web server control:
Composit Control Label Viewstate
Jan 29, 2011 02:04 PM|Shuvo Aymon|LINK
Jan 30, 2011 07:53 AM|Maxim Kornilov|LINK
This is the source code.
public class TestCompositeControl : CompositeControl
private Label testLabel = new Label();
private TextBox testTextBox = new TextBox();
private Button testButton = new Button();
#region Overrides of Control
/// Recreates the child controls in a control.
protected override void RecreateChildControls()
/// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
protected override void CreateChildControls()
this.testLabel.EnableViewState = true;
this.testLabel.ID = "testLabelId";
this.testTextBox.ID = "testTextBoxId";
this.testButton.ID = "testButtonId";
/// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.
protected override void OnInit(EventArgs e)
this.testButton.Click += new EventHandler(testButton_Click);
/// Handles the Click event of the testButton control.
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void testButton_Click(object sender, EventArgs e)
this.testLabel.Text = "This is test message.";
this.testTextBox.Text = "This is test message.";
Test web page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CompositeControlWithUpdatePanel._Default" %>
<%@ Register Assembly="CompositeControlWithUpdatePanel" Namespace="CompositeControlWithUpdatePanel.ServerControls" TagPrefix="wf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<form id="form1" runat="server">
<asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
<asp:UpdatePanel ID="UpdatePanelTest" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<wf:TestCompositeControl EnableViewState="false" ID="test" runat="server" /> <br />
<wf:TestCompositeControl EnableViewState="false" ID="test2" runat="server" />
Jan 30, 2011 08:10 AM|Maxim Kornilov|LINK
I found my mistake. It seems that during some of the test I set EnableViewState="false" to control and forget about it: <wf:TestCompositeControl EnableViewState="false" ID="test" runat="server" /> <br />.
Thank everyone for help.