Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Composite Control sequencing question

Reply
Thread Tools

Composite Control sequencing question

 
 
Cathead
Guest
Posts: n/a
 
      09-11-2003
I have a simple composite control (below) that renders a list of
words/phrases. I am adding words to the control within the Page_Load()
method of the containing page's code-behind (also below).

The control renders my word list fine for the initial request, but not on
postback. On postback, no words are displayed. I've come to realize that
CreateChildControls() is invoked earlier during a postback: before
Page_Load(), when there are not yet any words to display.

So obviously Page_Load is a bad place to add words to my composite control.
I've moved the code that adds words to the control to OnInit(), and it works
fine there, since it always runs before CreateChildControls() runs.

Given all of the above, my question is: When using a composite control that
overrides CreateChildControls() to render itself, is it standard procedure
to set up the control (i.e. add words) during the Init phase of the cycle
versus the other phases? Or is there another way?

It sounds like I'm answering my own question here, but I'm a newbie to
ASP.NET and am curious about what "best practices" are with regard to server
controls.

Regards,

CH

-----------------------
| The composite control |
-----------------------

public class TestControl : WebControl, INamingContainer
{
ArrayList _testList = new ArrayList();

/// <summary>
/// Adds a word or phrase to _textList
/// </summary>
public void AddText(string text)
{
_textList.Add(text);
}


/// <summary>
/// Overridden a la Composite control technique.
/// </summary>
protected override void CreateChildControls()
{
base.CreateChildControls();

Table table = new Table();
TableRow tr = null;

// Add each word/phrase within a new table row
foreach(string text in _textList)
{
tr = new TableRow();
table.Rows.Add(tr);
cell = new TableCell();
cell.Controls.Add(new LiteralControl(text));
tr.Cells.Add(cell);
}

this.Controls.Add(table);
}
}


------------------
| Code-behind page |
------------------

public class Test : Page
{
private void Page_Load(object sender, System.EventArgs e)
{
_testControl.AddText("Hello");
_testControl.AddText("World");
}
}


 
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
Losing Composite Control property that another Composite Control ... Chad ASP .Net Building Controls 0 02-01-2005 09:01 PM
IsPostBack Sequencing problem in dynamically created controls on web page. Kamal Jeet Singh ASP .Net 1 09-23-2004 04:18 PM
Possible to create a composite control that has a child control that is a validator that validates the composite control itself? Jonathan Eric Miller ASP .Net Building Controls 2 07-22-2004 10:58 PM
Composite control with dynamic composite controls sleigh ASP .Net 1 02-12-2004 06:24 PM
newbie question on sequencing Mar Thomas Java 2 08-27-2003 05:29 AM



Advertisments