Templated Data-Bound Control Sample

Discussion in 'ASP .Net Building Controls' started by news.microsoft.com, Aug 20, 2004.

  1. 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;
    }
    }
    }
    news.microsoft.com, Aug 20, 2004
    #1
    1. Advertising

  2. news.microsoft.com

    Sam Fields Guest

    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" <> wrote in message
    news:...
    > 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;
    > }
    > }
    > }
    >
    >
    >
    Sam Fields, Aug 20, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cosine... zero
    Replies:
    0
    Views:
    657
    cosine... zero
    Jun 9, 2004
  2. Replies:
    0
    Views:
    2,226
  3. Mario Krsnic
    Replies:
    0
    Views:
    376
    Mario Krsnic
    Jun 23, 2006
  4. joe_s

    Templated data-bound server control design question

    joe_s, Jul 24, 2003, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    171
    joe_s
    Jul 25, 2003
  5. Manu

    Data Bound Templated Control - design time problem

    Manu, May 22, 2006, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    112
Loading...

Share This Page