[thelist] dynamic tables and Page Life Cycle

Mark Joslyn Mark.Joslyn at SolimarSystems.com
Mon Feb 25 12:00:58 CST 2008

Howdy List.

I am having a bit of trouble figuring out the best way to handle dynamically
created controls.

Here is what I have,

1. I connect to a web service that returns me a DataSet. I create a dynamic
table from the DataSet. The first row is the Header row which contains
LinkButtons (for sorting).
2. In the Page_Load event, I call a function called CreateTable() -
CreateTable() calls code in a separate class file (TableBuilder) that builds
the table, sets up click event delegates, etc.. Here is some of the

private string _sortField
  get { return (ViewState["SortField"] as string != null) ?
ViewState["SortField"] as string : String.Empty; }
  set { ViewState["SortField"] = value; }

private bool _sortAsc
  get { return (ViewState["SortAscending"] != null) ?
(bool)ViewState["SortAscending"] : false; }
  set { ViewState["SortAscending"] = value; }

TableBuilder dataTable = new TableBuilder();

protected void Page_Load (object sender, EventArgs e)
  // Create Jobs data table

  if (!Page.IsPostBack)
    ... other stuff ...

public void CreateTable()
  // Create Table (this calls code in a separate class file (TableBuilder))
  dataTable.createTable(_sortAsc, _sortField);

  // Set up event handlers - from delegates in separate class file
  dataTable.headerClick += new TableBuilder.ClickHandler(pagerClick);

protected void headerClick(object sender, EventArgs e)
  // Toggle Sort Ascending Property (true:false) (ViewState)
  _sortAsc = (_sortAsc == false) ? true : false;

  // Set Sort Field Property to clicked header value (ViewState)
  _sortField = oBtn.CommandArgument;
  // Rebuild Jobs data table

Here is where I am confused. The page loads fine - once I click the header
row link buttons, it loads fine still, but the web service is going to be
hit twice - once on the Page_Load event then once on the Click Event. That
is not a good thing - overhead, multiple calls, etc.. If I place the
CreateTable() call in a !isPostBack, the page loads fine the first time, but
once I click the link button, the table disappears. If I take the
CreateTable() call out of the headerClick function, then the sorting happens
on the second click instead of the first click.

So here is what I would like to figure out:

1. Where do I put the CreateTable() call?
2. Where do I set up my ClickEvent handlers?
3. Do I call CreateTable() in my headerClick function?

I have read about the Page Life Cycle, but I still cannot seems to place the
items I need in the right places.

Any help would be GREAT!!!!




More information about the thelist mailing list