N
news.microsoft.com
I have been attempting to create a templated data-bound control, using the
sample as a guide
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpcontemplatedd
ataboundcontrolsample.htm). The problem that I'm running into is that child
controls in the template, such as labels, lose their data whenever the
control's visibility state is changed to false and then to true. I can
postback as many times as I want to, but as soon as that visibility changes
the data is no longer included in the viewstate. At first, I thought I had
implemented this incorrectly, but using the sample as-is from the help
produces the same result.
A simple test implementation of this is included below. Not included below
is the sample as-is (easy to copy-paste from the help to create -- didn't
want to make this post too big). Am I missing something simple here?
Thanks to any who can help.
Sam Fields
test.aspx
<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false"
Inherits="testproject.test" %>
<%@ Register TagPrefix="cc" Namespace="CustomControls"
Assembly="testproject" %>
<HTML>
<HEAD>
<title>Test Project</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<cc:TemplatedList id="tl" runat="server">
<ItemTemplate>
<asp:Label ID="lbl" Runat="server" text='<%# ((System.Data.DataRow)
Container.DataItem)[0] %>' />
</ItemTemplate>
</cc:TemplatedList>
<br>
<asp:Button ID="btntest" Runat="server" Text="Post Back" /><br>
<asp:Button ID="btnv" Runat="server" Text="Visible" />
</form>
</body>
</HTML>
test.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace testproject
{
/// <summary>
/// Summary description for test.
/// </summary>
public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btntest;
protected CustomControls.TemplatedList tl;
protected System.Web.UI.WebControls.Button btnv;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("test");
dt.Rows.Add(new object[] {"text value"} );
tl.DataSource = dt.Rows;
tl.DataBind();
}
// Put user code to initialize the page here
}
#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.btnv.Click += new System.EventHandler(this.btnv_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnv_Click(object sender, System.EventArgs e)
{
tl.Visible = !tl.Visible;
}
}
}
sample as a guide
(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpcontemplatedd
ataboundcontrolsample.htm). The problem that I'm running into is that child
controls in the template, such as labels, lose their data whenever the
control's visibility state is changed to false and then to true. I can
postback as many times as I want to, but as soon as that visibility changes
the data is no longer included in the viewstate. At first, I thought I had
implemented this incorrectly, but using the sample as-is from the help
produces the same result.
A simple test implementation of this is included below. Not included below
is the sample as-is (easy to copy-paste from the help to create -- didn't
want to make this post too big). Am I missing something simple here?
Thanks to any who can help.
Sam Fields
test.aspx
<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false"
Inherits="testproject.test" %>
<%@ Register TagPrefix="cc" Namespace="CustomControls"
Assembly="testproject" %>
<HTML>
<HEAD>
<title>Test Project</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<cc:TemplatedList id="tl" runat="server">
<ItemTemplate>
<asp:Label ID="lbl" Runat="server" text='<%# ((System.Data.DataRow)
Container.DataItem)[0] %>' />
</ItemTemplate>
</cc:TemplatedList>
<br>
<asp:Button ID="btntest" Runat="server" Text="Post Back" /><br>
<asp:Button ID="btnv" Runat="server" Text="Visible" />
</form>
</body>
</HTML>
test.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace testproject
{
/// <summary>
/// Summary description for test.
/// </summary>
public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btntest;
protected CustomControls.TemplatedList tl;
protected System.Web.UI.WebControls.Button btnv;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("test");
dt.Rows.Add(new object[] {"text value"} );
tl.DataSource = dt.Rows;
tl.DataBind();
}
// Put user code to initialize the page here
}
#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.btnv.Click += new System.EventHandler(this.btnv_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnv_Click(object sender, System.EventArgs e)
{
tl.Visible = !tl.Visible;
}
}
}