473,408 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Custom Control Appears to Interfere with ViewState

I have a very simple custom control that derives from WebControls.Panel and
implements INamingContainer.

It appear that controls created as children of my custom control are having
ViewState problems. For example, suppose I create a ListBox control as a
child of my custom control, attach a SelectedIndexChanged event handler to
it, and bind its data in the Page_Load event handler only if the current
request is not a post back. I load the page and initially everything is ok
and the list box has values. If I then select an item in the list box, the
page posts back to the server and thats when things get weird. The event
handler I orginally attached to SelectedIndexChanged doesn't get called and
upon further inspection the ListBox no longer has any items ... meaning they
were not loaded from ViewState.

If I take the ListBox in question *out* of my custom control, it works
perfectly fine. It also works if I put it inside a pure instance of
WebControls.Panel. So clearly something isn't right with my custom control.
I figure its something pretty obvious, but I'm new to custom controls ...
lucky me :)

Thought?

TIA//
Nov 19 '05 #1
3 1854
When and how are you creating/adding the controls in your custom
control? Make sure the controls are created upon each request (which
includes postbacks). I suggest override CreateChildControls and
create/add them there. Also override the Controls property, do a call
to EnsureChildControls and then return the base.Controls collection.

Nov 19 '05 #2
I have a custom control that is essentially implemented as follows:
================================================
[ ToolboxData("<{0}:TestPanel runat=server></{0}:TestPanel>") ]
[ ParseChildren( false ) ]
public class TestPanel : WebControl, INamingContainer
{
protected override void CreateChildControls()
{
this.Controls.AddAt( 0, new LiteralControl( "<span id=" + this.ID +
">" ) );
this.Controls.Add( new LiteralControl( "</span>" ) );
}
protected override void Render(HtmlTextWriter writer)
{
RenderChildren( writer );
}
}
================================================

In a given ASPX I have the following:
================================================
<cc1:TestPanel id=TestPanel1 runat="server">
<asp:ListBox id=ListBox1 runat="server" AutoPostBack="True">
</asp:ListBox>
</cc1:TestPanel>
================================================

The code behind for the ASPX has:
================================================
private void Page_Load(object sender, System.EventArgs e)
{
if( ! IsPostBack )
{
ListBox1.Items.Add( new ListItem( "1", "1" ) );
ListBox1.Items.Add( new ListItem( "2", "2" ) );
ListBox1.Items.Add( new ListItem( "3", "3" ) );
ListBox1.Items.Add( new ListItem( "4", "4" ) );
ListBox1.Items.Add( new ListItem( "5", "5" ) );
ListBox1.Items.Add( new ListItem( "6", "6" ) );
}
}

*Also assume that ListBox1 has its SelectedIndexChanged event wired to a
local handler.
================================================
When this ASPX initially loads, everything looks fine. However, if a browser
client then selectes an item in ListBox1 causing a post back, the ListBox
control's SelectedIndexChanged handler doesn't get called and none of the
orginal items are retrieved from ViewState. Inspecting the __VIEWSTATE field
sent to the client after initial page request and then the subsequent
postback showed that value goes from something relatively large to something
arbitrarily small (the digest/hash value of a blank ViewState I assume),
confirming to me that something is causing ViewState not work properly.

To compare, if you change the ASPX page to look like:
================================================
<asp:ListBox id=ListBox1 runat="server" AutoPostBack="True">
</asp:ListBox>
<cc1:TestPanel id=TestPanel1 runat="server">
</cc1:TestPanel>
================================================

.... removing the ListBox from the TestPanel container, things seem to work
fine; SelectedIndexChanged is called, the ListBox retains its items between
requests, and the __VIEWSTATE field doesn't seem to loose its value.

"Wilco Bauwer" <wi****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
When and how are you creating/adding the controls in your custom
control? Make sure the controls are created upon each request (which
includes postbacks). I suggest override CreateChildControls and
create/add them there. Also override the Controls property, do a call
to EnsureChildControls and then return the base.Controls collection.

Nov 19 '05 #3
First, drop the literal controls, just use the enumerations on the HTMLWriter
class, easier and keeps the control tree smaller, being faster.

This is most likely related to the order in which things are being
persisted. You, as a user, are adding a control to a custom control list.
You need to maintain that order yourself. In the Controls property, override
the property to have the following:

public override ControlCollection Controls {
get {
EnsureChildControls();
return base.Controls;
}
}

This code causes the child controls that you created in CreateChildControls
to be added to the control tree becore other controls you added, preservig
the order.

"Chris Newby" wrote:
I have a custom control that is essentially implemented as follows:
================================================
[ ToolboxData("<{0}:TestPanel runat=server></{0}:TestPanel>") ]
[ ParseChildren( false ) ]
public class TestPanel : WebControl, INamingContainer
{
protected override void CreateChildControls()
{
this.Controls.AddAt( 0, new LiteralControl( "<span id=" + this.ID +
">" ) );
this.Controls.Add( new LiteralControl( "</span>" ) );
}
protected override void Render(HtmlTextWriter writer)
{
RenderChildren( writer );
}
}
================================================

In a given ASPX I have the following:
================================================
<cc1:TestPanel id=TestPanel1 runat="server">
<asp:ListBox id=ListBox1 runat="server" AutoPostBack="True">
</asp:ListBox>
</cc1:TestPanel>
================================================

The code behind for the ASPX has:
================================================
private void Page_Load(object sender, System.EventArgs e)
{
if( ! IsPostBack )
{
ListBox1.Items.Add( new ListItem( "1", "1" ) );
ListBox1.Items.Add( new ListItem( "2", "2" ) );
ListBox1.Items.Add( new ListItem( "3", "3" ) );
ListBox1.Items.Add( new ListItem( "4", "4" ) );
ListBox1.Items.Add( new ListItem( "5", "5" ) );
ListBox1.Items.Add( new ListItem( "6", "6" ) );
}
}

*Also assume that ListBox1 has its SelectedIndexChanged event wired to a
local handler.
================================================
When this ASPX initially loads, everything looks fine. However, if a browser
client then selectes an item in ListBox1 causing a post back, the ListBox
control's SelectedIndexChanged handler doesn't get called and none of the
orginal items are retrieved from ViewState. Inspecting the __VIEWSTATE field
sent to the client after initial page request and then the subsequent
postback showed that value goes from something relatively large to something
arbitrarily small (the digest/hash value of a blank ViewState I assume),
confirming to me that something is causing ViewState not work properly.

To compare, if you change the ASPX page to look like:
================================================
<asp:ListBox id=ListBox1 runat="server" AutoPostBack="True">
</asp:ListBox>
<cc1:TestPanel id=TestPanel1 runat="server">
</cc1:TestPanel>
================================================

.... removing the ListBox from the TestPanel container, things seem to work
fine; SelectedIndexChanged is called, the ListBox retains its items between
requests, and the __VIEWSTATE field doesn't seem to loose its value.

"Wilco Bauwer" <wi****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
When and how are you creating/adding the controls in your custom
control? Make sure the controls are created upon each request (which
includes postbacks). I suggest override CreateChildControls and
create/add them there. Also override the Controls property, do a call
to EnsureChildControls and then return the base.Controls collection.


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ppyrstr | last post by:
I have been trying to figure out how to have a custom control that is irregular (non-rectangular) not interfere with other elements on the screen that may interfere with it's "regular" shape (the...
0
by: Emrah Gozcu | last post by:
I have a custom control tabstrib and it loads another custom control when clicked a tab. Tabstrib event fires well but the other custom control which is dynamicly loaded after clicking a tab,...
2
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid...
2
by: BluDog | last post by:
Hi I have a created a custom web control called ImageBrowser, extract is below: <Code> #Region "Properties" Public Property Images() As ImageCollection
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
0
by: AndrewF | last post by:
Hi there. I am royally stuck with this so any help would be greatly appreciated. Basically I have a set of classes that create composite controls for the user. One of these is an upload object...
0
by: Walter | last post by:
Hi, can someone please help me with my custom control viewstate problem....I haven't slept for hours trying to get this fixed. I am making two custom controls which will be included on a single...
5
by: Mark Olbert | last post by:
It appears that FormView controls require the >>exact<< same layout of controls and control types in the various templates in order to function properly. Failure to do so results in a "failure to...
2
by: Chris | last post by:
I have created a date picker user control. I want to have a boolean property, which I can set to determine whether the control defaults to today's date. I have created a property which I set to...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.