By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,934 Members | 1,449 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,934 IT Pros & Developers. It's quick & easy.

Custom Control Interfering with ViewState

P: n/a
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.
Why is this happening? What do I need to do to get child controls of my
container working?

TIA//

Nov 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.