At the stage of OnInit, all control objects are performing initialization, if added controls were not initialized at this stage, and even though the postback data contain the values of those controls, you won’t be able to obtain the user inputed values of those controls at later stage. e.g. PageLoad, OnClickHandler method.

In order to retrieve values that entered by users from dynamically added controls, those control objects have to be initialized at OnInit stage. The tricky part at that stage is that you couldn’t get persisted data of previous request from viewstate. The way I did to get around of that is to either modify the control’s postbackUrl which caused postback action to the same page with querystring attaching persisted data. Secondly, you can load the data from the database to generate your dynamic controls at that stage, OnInit.

Once dynamically added controls are initialized at OnInit stage, the postback data including user entered data in the form will fill up those controls at later stage in the pipeline.

Tags: , ,

admin on February 14th, 2008

Simple script to combine elements within nested list (A list’s elements are lists)

static void Main(string[] args) {   // initialize a list object with List<string> elements   List<List<string>> nestedList = new List<List<string>>();   // add an element to the main list object   nestedList.Add(new List() { "a", "b", "c" });   // add another element to the main list object    nestedList.Add(new List() { "d", "e", "f" });   // LINQ query to collect all the string elements from each   // element within the main list.   // ( see equivalent foreach loop below )   List<string> all = ( from n in nestedList                 from l in n select l).ToList();   // print it out   all.ForEach(new Action(delegate(string s) { Console.WriteLine(s); }));   // output: abcdef   Console.ReadLine(); }

The foreach loop way to combine all the elements.

// this is the traditional foreach loop for the LINQ // query above. foreach( List<string> n  in nestedList )     foreach( string l in n )         all.Add(l);

Tags: ,