Capturing panel contentcontrols on directevents or directmethods returns 0

  1. #1

    Capturing panel contentcontrols on directevents or directmethods returns 0

    Guys,

    I was able to add usercontrol to panel using the below:


            protected override void OnLoad(EventArgs e)
            {
     Control control = (Control)this.LoadControl(attribute.Path);
                this.FormatParamControlPanel.ContentControls.Add(control);
                
                this.FormatParamControlPanel.UpdateContent();
    
    }
    But when i try to capture the control on a directevents or directmethods the contentconttrols.count is always =0


       protected void InsertFormat(object sender, DirectEventArgs e)
            {
    
    Control ctl=FormatParamControlPanel.Controls[0];??? exception
    }
       [DirectMethod]
            public void InsertFormat()
            {
    Control ctl=FormatParamControlPanel.Controls[0];exception ???
    
    }
    Any ideas?
  2. #2

    adding the user control from markup

    Adding the usercontrol in the markup allow me to capture the controls on direct event.
    But when adding them in code behind using the below doesn't work:

    var ctl = this.LoadControl("WebUserControl1.ascx");
    Pnl.ContentControls.Add(ctl);
    Pnl.UpdateContent();

    Am i missing a property or something?

    Thanx
  3. #3
    You are missing the design of asp.net - you need to restore the control tree on every postback.

    If you place a control in aspx/ascx markup then the asp.net compiler generates code which recreates the controls during the oninit event.

    If you create a control manually in code, well, there is no magic which automatically recreates the control on the next postback; you usally save some variable(s) in viewstate to remember how to recreate your controls and do this in the onload event (since in oninit the viewstate isn't available yet). Note that if you use automatically generated control ids you need to recreate the controls in the exact same order every time, or their generated control ids will differ. It's usually easier to set control ids manually.

    (Ext.Net DirectEvents do not behave exactly like normal postbacks, but as far as the control tree and viewstate is concerned, you still need to recreate the controls.)

    Here are links to a blog series about correctly creating dyanmic controls in code:
    http://weblogs.asp.net/infinitiesloo...t-1_2900_.aspx
    http://weblogs.asp.net/infinitiesloo...t-2_2900_.aspx
    http://weblogs.asp.net/infinitiesloo...t-3_2900_.aspx
    http://weblogs.asp.net/infinitiesloo...t-4_2900_.aspx

Similar Threads

  1. DirectEvents property on Panel tools
    By thedarklord in forum 1.x Help
    Replies: 0
    Last Post: Apr 18, 2012, 8:59 AM
  2. Replies: 3
    Last Post: May 19, 2011, 10:13 AM
  3. Date field returns nothing
    By Nime in forum 1.x Help
    Replies: 1
    Last Post: Nov 10, 2009, 8:35 AM
  4. [CLOSED] datefield always returns min value
    By speedstepmem4 in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Apr 30, 2009, 8:22 AM
  5. [CLOSED] dateRenderer returns NaN-NaN-NaN
    By Jurke in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Oct 01, 2008, 8:31 AM

Posting Permissions