Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Templated Data-Bound Control Sample

Reply
Thread Tools

Templated Data-Bound Control Sample

 
 
news.microsoft.com
Guest
Posts: n/a
 
      08-20-2004
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;
}
}
}



 
Reply With Quote
 
 
 
 
Sam Fields
Guest
Posts: n/a
 
      08-20-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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;
> }
> }
> }
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling templated member of templated object david@sunlightd.com C++ 1 06-22-2007 05:13 PM
templated function as parameter of another templated function Amadeus W. M. C++ 2 07-04-2006 09:59 PM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments