Custom control ViewState problem within a DataList
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcontrolscs' list.


Michael
I have a simple control, which inherits the Button Webcontrol and sets it
text to the number of times it's been clicked. When added directly to a
webform it seems to function perfectly. However if it is included within a
DataList there are problems. Firstly the text on the button is not being
set. Secondly from inspecting the ViewState in the debugger after each
round trip it seems the entry that was created in the Viewstate on the
previous roundtrip has disappeared i.e. ViewState["MyData"] is null. I'm at
a loss for an explanation of either of these behaviours. Does anyone have
an explanation ?

I'm guessing I am missing some code in my control...
Is there something about the relation between the control hierarchy and the
viewstate I'm not understanding here ?.

MyControl.cs (within an assembly called TestControl):-

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

namespace CustomControls
{
public class MyButton : System.Web.UI.WebControls.Button
{
public int MyData
{
get
{
object objectData = ViewState["MyData"];
if (objectData == null)
return 0;
else
return (int)objectData;
}

set
{
ViewState["MyData"] = value;
Text = value.ToString();
}
}

protected override void OnClick(EventArgs e)
{
++MyData;
}
}
}

Test Application:-
Webform5.aspx:-

<%@ Page language="c#" Codebehind="WebForm5.aspx.cs" AutoEventWireup="false"
Inherits="Test.WebForm5" %>
<%@ Register TagPrefix="cc1" Namespace="CustomControls"
Assembly="TestControl" %>
<HTML>
<body MS_POSITIONING="GridLayout">
<form id="WebForm5" method="post" runat="server">
<asp:DataList id="DataList1" style="Z-INDEX: 101; LEFT: 80px; POSITION:
absolute; TOP: 86px" runat="server">
<ItemTemplate>
<cc1:MyButton id="MyButton2" runat="server" Width="113px"
Height="26px"></cc1:MyButton>
<asp:Label id=Label1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem") %>'>
</asp:Label>
</ItemTemplate>
</asp:DataList>
<cc1:MyButton id="MyButton1" style="Z-INDEX: 102; LEFT: 83px; POSITION:
absolute; TOP: 34px" runat="server" Width="112px"
Height="27px"></cc1:MyButton>
</form>
</body>
</HTML>

Code behind Webform5.aspx.cs:-

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

namespace Test
{
/// <summary>
/// Summary description for WebForm5.
/// </summary>
public class WebForm5 : System.Web.UI.Page
{
protected CustomControls.MyButton MyButton1;
protected System.Web.UI.WebControls.DataList DataList1;

private void Page_Load(object sender, System.EventArgs e)
{
StringCollection myData = new StringCollection();
myData.Add("Fruit");
myData.Add("Milk");
myData.Add("Vegetables");
DataList1.DataSource = myData;
DataList1.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
Michael Lang
MS .NET Developer
Ph: +61 0417498620
www.michaellang.com.au
Reply to this message...
 
    
Vandana Datye (DevCepts)
Because you're data binding on each request the DataList is not restored =
using the saved view state, but recreated every time using the data =
source. This means that controls in the template are also not restored =
using the saved view state. Try placing your data binding code in =
Page_Load in a
if (!IsPostBack) { ...} clause to recreate the DataList using the view =
state mechanism.

Vandana=20
-----Original Message-----
From: Michael [mailto:Click here to reveal e-mail address]=20
Sent: Sunday, April 21, 2002 8:33 PM
To: aspngcontrolscs
Subject: [aspngcontrolscs] Custom control ViewState problem within a =
DataList

I have a simple control, which inherits the Button Webcontrol and sets =
it text to the number of times it's been clicked.=A0=A0When added =
directly to a webform it seems to function perfectly.=A0 However if it =
is included within a DataList there are problems.=A0=A0Firstly the text =
on the button is=A0not being set.=A0 Secondly from inspecting the =
ViewState in the debugger after each round trip it seems the entry that =
was created in the Viewstate=A0on the previous roundtrip has disappeared =
i.e. ViewState["MyData"]=A0is null.=A0 I'm at a loss for an explanation =
of either of these behaviours.=A0 Does anyone have an explanation ?
=A0
I'm guessing I am missing some code in my control...=20
Is there something about the relation between the control hierarchy and =
the viewstate I'm not understanding here ?.
=A0
MyControl.cs (within an assembly called TestControl):-
=A0
using System;
using System.Web.UI;
using System.ComponentModel;
=A0
namespace CustomControls
{
=A0public class MyButton : System.Web.UI.WebControls.Button
=A0{
=A0=A0public int MyData=20
=A0=A0{
=A0=A0=A0get=20
=A0=A0=A0{
=A0=A0=A0=A0object objectData =3D ViewState["MyData"];
=A0=A0=A0=A0if (objectData =3D=3D null)
=A0=A0=A0=A0=A0return 0;
=A0=A0=A0=A0else
=A0=A0=A0=A0=A0return (int)objectData;
=A0=A0=A0}
=A0
=A0=A0=A0set
=A0=A0=A0{
=A0=A0=A0=A0ViewState["MyData"] =3D value;
=A0=A0=A0=A0Text =3D value.ToString();
=A0=A0=A0}
=A0=A0}
=A0
=A0=A0protected override void OnClick(EventArgs e)
=A0=A0{
=A0=A0=A0++MyData;
=A0=A0}
=A0}
}
Test Application:-
Webform5.aspx:-
=A0
<%@ Page language=3D"c#" Codebehind=3D"WebForm5.aspx.cs" =
AutoEventWireup=3D"false" Inherits=3D"Test.WebForm5" %>
<%@ Register TagPrefix=3D"cc1" Namespace=3D"CustomControls" =
Assembly=3D"TestControl" %>
<HTML>
=A0<body MS_POSITIONING=3D"GridLayout">
=A0=A0<form id=3D"WebForm5" method=3D"post" runat=3D"server">
=A0=A0=A0<asp:DataList id=3D"DataList1" style=3D"Z-INDEX: 101; LEFT: =
80px; POSITION: absolute; TOP: 86px" runat=3D"server">
=A0=A0=A0=A0<ItemTemplate>
=A0=A0=A0=A0=A0<cc1:MyButton id=3D"MyButton2" runat=3D"server" =
Width=3D"113px" Height=3D"26px"></cc1:MyButton>
=A0=A0=A0=A0=A0<asp:Label id=3DLabel1 runat=3D"server" Text=3D'<%# =
DataBinder.Eval(Container, "DataItem") %>'>
=A0=A0=A0=A0=A0</asp:Label>
=A0=A0=A0=A0</ItemTemplate>
=A0=A0=A0</asp:DataList>
=A0=A0=A0<cc1:MyButton id=3D"MyButton1" style=3D"Z-INDEX: 102; LEFT: =
83px; POSITION: absolute; TOP: 34px" runat=3D"server" Width=3D"112px" =
Height=3D"27px"></cc1:MyButton>
=A0=A0</form>
=A0</body>
</HTML>
Code behind Webform5.aspx.cs:-
=A0
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
=A0
namespace Test
{
=A0/// <summary>
=A0/// Summary description for WebForm5.
=A0/// </summary>
=A0public class WebForm5 : System.Web.UI.Page
=A0{
=A0=A0protected CustomControls.MyButton MyButton1;
=A0=A0protected System.Web.UI.WebControls.DataList DataList1;
=A0
=A0=A0private void Page_Load(object sender, System.EventArgs e)
=A0=A0{
=A0=A0=A0StringCollection myData =3D new StringCollection();
=A0=A0=A0myData.Add("Fruit");
=A0=A0=A0myData.Add("Milk");
=A0=A0=A0myData.Add("Vegetables");
=A0=A0=A0DataList1.DataSource =3D myData;
=A0=A0=A0DataList1.DataBind();
=A0=A0}
=A0
=A0=A0#region Web Form Designer generated code
=A0=A0override protected void OnInit(EventArgs e)
=A0=A0{
=A0=A0=A0//
=A0=A0=A0// CODEGEN: This call is required by the ASP.NET Web Form =
Designer.
=A0=A0=A0//
=A0=A0=A0InitializeComponent();
=A0=A0=A0base.OnInit(e);
=A0=A0}
=A0=A0
=A0=A0/// <summary>
=A0=A0/// Required method for Designer support - do not modify
=A0=A0/// the contents of this method with the code editor.
=A0=A0/// </summary>
=A0=A0private void InitializeComponent()
=A0=A0{=A0=A0=A0=20
=A0=A0=A0this.Load +=3D new System.EventHandler(this.Page_Load);
=A0
=A0=A0}
=A0=A0#endregion
=A0}
}
Top of Form
Bottom of Form
Michael Lang
MS .NET Developer
Ph: +61 0417498620
www.michaellang.com.au
=A0
| [aspngcontrolscs] member Click here to reveal e-mail address =3D YOUR ID | =
http://www.asplists.com/asplists/aspngcontrolscs.asp =3D JOIN/QUIT | =
http://www.asplists.com/search =3D SEARCH Archives=20

Reply to this message...
 
    
david larlick
I am having a similiar problem. Please post what you have found out or your resolution to the situation.

Thanks !!

--------------------------------
From: david larlick
Reply to this message...
 
 
System.Collections.Specialized.StringCollection
System.EventArgs
System.EventHandler
System.Web.UI.DataBinder
System.Web.UI.Page
System.Web.UI.WebControls.Button
System.Web.UI.WebControls.DataList




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com