CompositeDataBoundControl doesn't retreive it's state information.

Discussion in 'ASP .Net' started by Umut Tezduyar, Jul 5, 2006.

  1. I have a control derived from ComposeteDataBoundControl. Control doesn't
    retreive it's state information. I am going to frick out! What should I do
    more?
    I added two codes. One is for Default.aspx to Test the control
    "SampleDataBound". When I click to the "Refresh" button here, the data
    displayed in the SampleDataBound control become lost.

    ----------------------------------------

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server" type="text/C#">


    protected override void OnLoad(EventArgs e)

    {

    base.OnLoad(e);

    if (!Page.IsPostBack)

    {

    System.Collections.Generic.List<string> list = new
    System.Collections.Generic.List<string>();

    list.Add("UMUT");

    list.Add("Mehmet");

    list.Add("Hasan");

    this.d.DataSource = list;

    this.d.DataBind();

    }

    }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title>Untitled Page</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <Test:SampleDataBound runat="server" ID="d" EnableViewState="true" />

    </div>

    <asp:Button runat="server" ID="btn" Text="Refresh" />

    </form>

    </body>

    </html>



    ---------------------------------------

    public class SampleDataBound : CompositeDataBoundControl, INamingContainer

    {

    private Table table;

    protected override int
    CreateChildControls(System.Collections.IEnumerable dataSource, bool
    dataBinding)

    {

    System.Collections.IEnumerator e = dataSource.GetEnumerator();

    table = new Table();

    this.Controls.Add(table);

    TableRow row = null;

    TableCell cell = null;

    int count = 0;

    while (e.MoveNext())

    {

    row = new TableRow();

    cell = new TableCell();

    if (dataBinding)

    {

    cell.Text = e.Current.ToString();

    }

    row.Cells.Add(cell);

    table.Rows.Add(row);

    count++;

    }

    return count;

    }

    }
    Umut Tezduyar, Jul 5, 2006
    #1
    1. Advertising

  2. Umut Tezduyar

    Teemu Keiski Guest

    Hi,

    you have lines

    row = new TableRow();
    cell = new TableCell();
    if (dataBinding)
    {
    cell.Text = e.Current.ToString();
    }
    row.Cells.Add(cell);
    table.Rows.Add(row);

    put them in different order

    row = new TableRow();
    table.Rows.Add(row);
    cell = new TableCell();
    row.Cells.Add(cell);

    if (dataBinding)
    {
    cell.Text = e.Current.ToString();
    }

    e.g do databinding after cell is added to the Rows and so on.

    Reason is that a ontrol like TableCell starts its lifecycle when it's added
    to the Controls collection (even though here it's to Cells, but that's
    basically just abstraction) and part of this lifecycle is to start tracking
    state.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    "Umut Tezduyar" <> wrote in message
    news:...
    >I have a control derived from ComposeteDataBoundControl. Control doesn't
    > retreive it's state information. I am going to frick out! What should I do
    > more?
    > I added two codes. One is for Default.aspx to Test the control
    > "SampleDataBound". When I click to the "Refresh" button here, the data
    > displayed in the SampleDataBound control become lost.
    >
    > ----------------------------------------
    >
    > <%@ Page Language="C#" %>
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >
    > <script runat="server" type="text/C#">
    >
    >
    > protected override void OnLoad(EventArgs e)
    >
    > {
    >
    > base.OnLoad(e);
    >
    > if (!Page.IsPostBack)
    >
    > {
    >
    > System.Collections.Generic.List<string> list = new
    > System.Collections.Generic.List<string>();
    >
    > list.Add("UMUT");
    >
    > list.Add("Mehmet");
    >
    > list.Add("Hasan");
    >
    > this.d.DataSource = list;
    >
    > this.d.DataBind();
    >
    > }
    >
    > }
    >
    > </script>
    >
    > <html xmlns="http://www.w3.org/1999/xhtml">
    >
    > <head runat="server">
    >
    > <title>Untitled Page</title>
    >
    > </head>
    >
    > <body>
    >
    > <form id="form1" runat="server">
    >
    > <div>
    >
    > <Test:SampleDataBound runat="server" ID="d" EnableViewState="true" />
    >
    > </div>
    >
    > <asp:Button runat="server" ID="btn" Text="Refresh" />
    >
    > </form>
    >
    > </body>
    >
    > </html>
    >
    >
    >
    > ---------------------------------------
    >
    > public class SampleDataBound : CompositeDataBoundControl, INamingContainer
    >
    > {
    >
    > private Table table;
    >
    > protected override int
    > CreateChildControls(System.Collections.IEnumerable dataSource, bool
    > dataBinding)
    >
    > {
    >
    > System.Collections.IEnumerator e = dataSource.GetEnumerator();
    >
    > table = new Table();
    >
    > this.Controls.Add(table);
    >
    > TableRow row = null;
    >
    > TableCell cell = null;
    >
    > int count = 0;
    >
    > while (e.MoveNext())
    >
    > {
    >
    > row = new TableRow();
    >
    > cell = new TableCell();
    >
    > if (dataBinding)
    >
    > {
    >
    > cell.Text = e.Current.ToString();
    >
    > }
    >
    > row.Cells.Add(cell);
    >
    > table.Rows.Add(row);
    >
    > count++;
    >
    > }
    >
    > return count;
    >
    > }
    >
    > }
    >
    >
    >
    >
    >
    >
    Teemu Keiski, Jul 5, 2006
    #2
    1. Advertising

  3. God Bless you Teemu Keiski. I can't explain how I am happy now.

    "Teemu Keiski" <> wrote in message
    news:...
    > Hi,
    >
    > you have lines
    >
    > row = new TableRow();
    > cell = new TableCell();
    > if (dataBinding)
    > {
    > cell.Text = e.Current.ToString();
    > }
    > row.Cells.Add(cell);
    > table.Rows.Add(row);
    >
    > put them in different order
    >
    > row = new TableRow();
    > table.Rows.Add(row);
    > cell = new TableCell();
    > row.Cells.Add(cell);
    >
    > if (dataBinding)
    > {
    > cell.Text = e.Current.ToString();
    > }
    >
    > e.g do databinding after cell is added to the Rows and so on.
    >
    > Reason is that a ontrol like TableCell starts its lifecycle when it's
    > added to the Controls collection (even though here it's to Cells, but
    > that's basically just abstraction) and part of this lifecycle is to start
    > tracking state.
    >
    > --
    > Teemu Keiski
    > ASP.NET MVP, AspInsider
    > Finland, EU
    > http://blogs.aspadvice.com/joteke
    >
    > "Umut Tezduyar" <> wrote in message
    > news:...
    >>I have a control derived from ComposeteDataBoundControl. Control doesn't
    >> retreive it's state information. I am going to frick out! What should I
    >> do
    >> more?
    >> I added two codes. One is for Default.aspx to Test the control
    >> "SampleDataBound". When I click to the "Refresh" button here, the data
    >> displayed in the SampleDataBound control become lost.
    >>
    >> ----------------------------------------
    >>
    >> <%@ Page Language="C#" %>
    >>
    >> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    >> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >>
    >> <script runat="server" type="text/C#">
    >>
    >>
    >> protected override void OnLoad(EventArgs e)
    >>
    >> {
    >>
    >> base.OnLoad(e);
    >>
    >> if (!Page.IsPostBack)
    >>
    >> {
    >>
    >> System.Collections.Generic.List<string> list = new
    >> System.Collections.Generic.List<string>();
    >>
    >> list.Add("UMUT");
    >>
    >> list.Add("Mehmet");
    >>
    >> list.Add("Hasan");
    >>
    >> this.d.DataSource = list;
    >>
    >> this.d.DataBind();
    >>
    >> }
    >>
    >> }
    >>
    >> </script>
    >>
    >> <html xmlns="http://www.w3.org/1999/xhtml">
    >>
    >> <head runat="server">
    >>
    >> <title>Untitled Page</title>
    >>
    >> </head>
    >>
    >> <body>
    >>
    >> <form id="form1" runat="server">
    >>
    >> <div>
    >>
    >> <Test:SampleDataBound runat="server" ID="d" EnableViewState="true" />
    >>
    >> </div>
    >>
    >> <asp:Button runat="server" ID="btn" Text="Refresh" />
    >>
    >> </form>
    >>
    >> </body>
    >>
    >> </html>
    >>
    >>
    >>
    >> ---------------------------------------
    >>
    >> public class SampleDataBound : CompositeDataBoundControl,
    >> INamingContainer
    >>
    >> {
    >>
    >> private Table table;
    >>
    >> protected override int
    >> CreateChildControls(System.Collections.IEnumerable dataSource, bool
    >> dataBinding)
    >>
    >> {
    >>
    >> System.Collections.IEnumerator e = dataSource.GetEnumerator();
    >>
    >> table = new Table();
    >>
    >> this.Controls.Add(table);
    >>
    >> TableRow row = null;
    >>
    >> TableCell cell = null;
    >>
    >> int count = 0;
    >>
    >> while (e.MoveNext())
    >>
    >> {
    >>
    >> row = new TableRow();
    >>
    >> cell = new TableCell();
    >>
    >> if (dataBinding)
    >>
    >> {
    >>
    >> cell.Text = e.Current.ToString();
    >>
    >> }
    >>
    >> row.Cells.Add(cell);
    >>
    >> table.Rows.Add(row);
    >>
    >> count++;
    >>
    >> }
    >>
    >> return count;
    >>
    >> }
    >>
    >> }
    >>
    >>
    >>
    >>
    >>
    >>

    >
    >
    Umut Tezduyar, Jul 6, 2006
    #3
    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. Umut Tezduyar
    Replies:
    0
    Views:
    395
    Umut Tezduyar
    Jun 29, 2006
  2. Umut Tezduyar
    Replies:
    0
    Views:
    694
    Umut Tezduyar
    Jul 2, 2006
  3. Griff
    Replies:
    1
    Views:
    816
    Michael Hamrah
    Oct 12, 2006
  4. Jos
    Replies:
    1
    Views:
    157
  5. Samuel

    compositedataboundcontrol with template databinding problem

    Samuel, Mar 23, 2006, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    244
    Steven Cheng[MSFT]
    Mar 24, 2006
Loading...

Share This Page