CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

MS SQL 2008 on ASP.NET Hosting



Zone: > NEWSGROUP > Asp.Net Forum > windows_hosting.hosting_open_forum Tags:
Item Type: NewsGroup Date Entered: 9/29/2004 12:48:08 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 1 Views: 76 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
2 Items, 1 Pages 1 |< << Go >> >|
michiel
Asp.Net User
ViewState not saved in Panel child controls9/29/2004 12:48:08 PM

0/0

I'm trying to make a control where I can add panels that become (in)visible under certain circumstances. As a prototype I created a test control that holds panels as child controls, which can contain child controls. To load the panels, I created a ControlBuilder.

Everything works fine, except for one thing: the ViewState of child controls within the panels is not retained. I checked the whole cycle, and all seems well... values on the controls change, etc. etc. But when the page reloads after a postback, no ViewState is loaded.

The wierdest thing is that to track what happens inside a TextBox in one of the panels, I created a control that inherits from TextBox. If I only inherit (as follows), the inherited control retains i's ViewState, while the TextBox does not!

public class TestBox : System.Web.UI.WebControls.TextBox {}


I have included the full prototype code below, and I hope someone can shed some light on the matter. BTW, I know controls that are not visible by default do not save ViewState, so I already tried code to override this behavior. That did not work, and judging from the TestBox example, that shouldn't be necessary anyway in this solution.

The C# code

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dev.WebControls
{
[ParseChildren(false)]
[ControlBuilder(typeof(TestControlBuilder))]
public class TestControl : WebControl, IPostBackEventHandler
{
protected int SelectedPanelIndex
{
get
{
object o = ViewState["SelectedPanelIndex"];
if(o == null) return 0;
return (int)o;
}
set
{
ViewState["SelectedPanelIndex"]= value;
}
}

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

protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
for(int i = 0; i < this.Controls.Count; i++)
{
Panel panel = (Panel)this.Controls[i];
writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.GetPostBackClientHyperlink(this, i.ToString()));
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.Write(panel.ID);
writer.RenderEndTag();
if(this.SelectedPanelIndex != i)
{
panel.Visible = false;
}
writer.WriteFullBeginTag("br");
}
writer.WriteFullBeginTag("hr");

base.RenderContents(writer);

foreach(Control control in this.Controls)
{
if(control is Panel) control.Visible = true;
}
}

#region IPostBackEventHandler Members

public void RaisePostBackEvent(string eventArgument)
{
this.SelectedPanelIndex = Convert.ToInt32(eventArgument);
}

#endregion
}

public class TestControlBuilder : ControlBuilder
{
public override bool AllowWhitespaceLiterals()
{
return false;
}

public override Type GetChildControlType(string tagName, System.Collections.IDictionary attribs)
{
if(string.Compare(tagName, "asp:Panel", true) == 0)
{
return typeof(Panel);
}
else
{
throw new ApplicationException("Only panels are allowed as child controls.");
}
}
}
}


The ASPX

<%@ Register TagPrefix="cc1" Namespace="Dev.WebControls" Assembly="Dev.WebControls" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<body>
<form id="Form1" method="post" runat="server">
<cc1:TestControl id="TestControl1" runat="server">
<asp:panel id="Panel1" runat="server">
<cc1:TestBox ID="TestBox1" Runat="server">123</cc1:TabBox>
<asp:TextBox ID="Textbox1" Runat="server">456</asp:TextBox>
</asp:panel>
<asp:panel id="Panel2" runat="server">
<asp:TextBox ID="Textbox2" Runat="server">789</asp:TextBox>
</asp:panel>
</cc1:TestControl>
</form>
</body>
</HTML>

Michiel van Otegem

http://www.aspnl.com
http://www.aspalliance.com/michiel
---
Teach Yourself XSLT in 21 Days
http://www.amazon.com/exec/obidos/ASIN/0672323184/aspnlcom-20
michiel
Asp.Net User
Re: ViewState not saved in Panel child controls10/11/2004 5:50:36 AM

0/0

The solution to my problem turned out to be simple... I am changing visibility of panels during the Render cycle, but because ViewState is saved before that, this doesn't affect the ViewState. Since the TextBox is visible in when ViewState is saved, it doesn't need to save ViewState, not knowing it will be invisible when it is actually rendered.

But what about the different behavior from the inherited TextBox. Nikhil Kothari from the ASP.NET team told me that they can't know what you changed to the control, so they can't be sure that you don't need to save ViewState like the original TextBox. To make sure it works properly, they assume you need to store ViewState always.
Michiel van Otegem

http://www.aspnl.com
http://www.aspalliance.com/michiel
---
Teach Yourself XSLT in 21 Days
http://www.amazon.com/exec/obidos/ASIN/0672323184/aspnlcom-20
2 Items, 1 Pages 1 |< << Go >> >|


Free Download:

Books:
Developing Microsoft ASP.NET Server Controls and Components Authors: Nikhil Kothari, Vandana Datye, Pages: 689, Published: 2002
Pro ASP.NET Web Forms Techniques Authors: Alex Homer, Pages: 580, Published: 2003

Web:
ViewState not saved in Panel child controls - ASP.NET Forums BTW, I know controls that are not visible by default do not save ViewState, so I already tried code to override this behavior. ...
CodeProject: Managing ViewState, Rendering and Events in Composite ... No, the columns are not saved with ViewState. They're child controls of the grid .... I have custom datagrid control within another custom panel control. ...
Controls and ViewState out of sync (?) - .NET ASP rendered to the browser, but it's not saved in the ViewState; as a .... You could get the viewstate of the child controls (by getting ...
Scott on Writing The gist of the section says that composite controls don't need to employ extra effort to save the view state of their children since it's saved ...
Custom DataGridColumn DataGridItems not saving to ViewState. Not ... is not saved to viewstate and after postback, the column does not contain data. ..... In the code below, child controls were being added to the cell only in ...
Panel Members (System.Web.UI.WebControls) If it does not, it creates child controls. (Inherited from Control.) .... the current server control's child controls have any saved view-state settings. ...
ViewState does not persist for childcontrols of compositecontrol ... 3, Viewstate is not maintained for the inner controls if i used Enabled/di… ..... 2. when not databinding, create dummy children just to receive viewstate. ...
Nikhil Kothari's Weblog : ViewState Notes While not immediately apparent, this pattern occurs quite frequently: a Panel with many of child controls of which just one has non-null view state, ...
Restore ViewState data with ViewStateModeById Feb 19, 2007 ... NET finds the controls when ViewState data restored. .... the label will not display "Click me!". The save ViewState became:. ...
Dynamic Controls, Viewstate, and EventHandlers - ASP.NET Forums I understand that when I call the Add() for the UpdatePanel, part of that call involves loading the ViewState back into the child controls ...




Search This Site:










advice -> i need a tabbed panel

accessing the datagrid with vsz file

handling events in custom controls

server control properties.... by value? or by reference?

support validation

usercontrol problem

control creation in oninit sub

opinion on web1host?

sql server 2005 express edition hosting!!!!!!!!!!!!

host recommendations

user web control not rendering if dynamically created

user control state problem

using javascript and styles in designer

system.web.ui.design.controldesigner ????

composite/template control

problem with datagrid checkbox column checkedchange event

childcontrolscreated and system.nullreferenceexception

events in http pipe not firing correctly, need help from a guru.

multiple rows (datalist details)

applying styles to button

custom control with two data sources

looking for free asp.net hosting for research

slide control needed

components question

hosting/uploading my site?

citrix hosting site?

did they improve the treeview control for 2.0?

browse folder dialog box.

compositedataboundcontrol

postback blues

  Privacy | Contact Us
All Times Are GMT