Templated Data-Bound Control Sample

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;
}
}
}
 
S

Sam Fields

Figured it out. Seems that when a control is invisible, CreateChildControls
is never called (seems obvious now, but it wasn't immediately apparent
before). I overrode the Controls property, and added an EnsureChildControls
to the get accessor. Now, it forces the child controls to be created before
it even decides whether or not it's actually going to Render.

Big thanks to Paul Wilson for writing the excellent viewstate article at
http://aspalliance.com/articleViewer.aspx?aId=135&pId=. I was able to see
what was in the viewstate and track down exactly why the controls were not
posting to the viewstate.

Thanks,
Sam Fields

news.microsoft.com said:
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;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top