locked
CompositeControl control-state RRS feed

  • Question

  • User-1387801607 posted

    Dear All,

    On my page I have a dynamically created CompositeControl which contains a number of child controls.

    I am looking for an event/method inside the CompositeControl which gets called after control state has been restored.

    1) On my page, during onload, I create the CompositControl.

    2) Within the CompositeControl the CreateChildControls method gets called.

       (no control state available yet)

    3) ..an event or method within the CompositControl after control-state has been restored for its child controls..

    4) Control-event on the page fires (from button which cause postback)

     

    I hope somebody can help me.

    Thanks in advance!

    Bas

    Thursday, January 12, 2012 5:47 AM

All replies

  • User3866881 posted

    Hello:)

    Please show us your finished sample codes so as to let us know how your composite control is。

    Best reguards!

    Friday, January 13, 2012 8:08 PM
  • User-1387801607 posted

    Hi Decker Dong, thanks for your reply!

    Let me try to give you some pieces of the composite control so all the additional stuff, which has nothing to do with the question, will not cause a distraction.

    The working of the control is as follows:

    On a page the control is displayed as an image.

    Each time the user clicks on the image a different image is being displayed.

    The url’s of all possible images are stored in an array.

    After each click the array-index of the current url is being stored in a hidden field.

    After a postback the value of this hidden field is what is being used by server side code.

     

    Some code from the control itself:

    // SwitchValue - new property
    [Category("Appearance")]   
    [Themeable(true)]
    public int SwitchValue
    {
        get
        {
            if (hfCurValue.Value == String.Empty)
               return -1;
            else
               return Convert.ToInt32(hfCurValue.Value);
        }
        set
        {
            hfCurValue.Value = value.ToString();
        }
    }
    
    
    // Private variable //
    private string[] ImageUlrMap;
    // Controls //
    private System.Web.UI.WebControls.Image imgMain;
    private HiddenField hfCurValue;
    
    
    // Create ChildControls //
    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        imgMain = new System.Web.UI.WebControls.Image();
        hfCurValue = new HiddenField();
    
        // Init SwitchValue //
        if (SwitchValue == -1)
            SwitchValue = 0;
    
    
        // Set Image Initial Url (ref: SetImage) //
        string sURL = ThemePath + ImageUlrMap[SwitchValue];
        imgMain.ImageUrl = sURL;
    
    
        // Add the Image-control to the Controls collection //
        this.Controls.Add(imgMain);
        // Add the HiddenField-control to the Controls collection //
        this.Controls.Add(hfCurValue);
    
    }
    
    

    Some code of the Some code to generate the composite control:

    protected void Page_Load(object sender, EventArgs e)
    {
        // Create Control //
        HxSwitchButtonCS.HxSwitchButtonCS hxsbSwitch = new     HxSwitchButtonCS.HxSwitchButtonCS();
        hxsbSwitch.ID = "hxsbSwitch";
        hxsbSwitch.ClientClass = "SwitchGroup";
        hxsbSwitch.SwitchGroup = "Switch";
        hxsbSwitch.ThemePath = "";
        hxsbSwitch.ImageUrl = "images/switch_0.png;images/switch_1.png;images/switch_2.png ";
        hxsbSwitch.HoverImageUrl = "images/switch_0_h.png;images/switch_1_h.png;images/switch_2_h.png ";
    
        PlaceHolder1.Controls.Add(hxsbSwitch);
    }
    

     

    This all works very nice (in the above code I left out JS). My problem is the following:

    If I add a button to the page, with no code behind it, just to cause a postback, obviously CreateChildControls is being called.

    CreateChildControls will set the image url (ref: SetImage) depending on the value of the hidden field.

    But in this stage, the value of the hidden filed is always empty as control state has not yet been restored.

     

    What I think I need is to move the part of the code where the image url is being set (ref: SetImage) to some overridden method which will get called after control state has been restored.

     

    Wednesday, January 18, 2012 7:03 AM
  • User3866881 posted

    If I add a button to the page, with no code behind it, just to cause a postback, obviously CreateChildControls is being called.

    Hello again:)

    Yes,that's the question and problem——don't forget to set ChildControlsCreated=true in the CreateChildrControls at the end of your overridden codes……

    Reguards! 

    Wednesday, January 18, 2012 8:01 PM
  • User-1387801607 posted

    Yes, of course! At the end of the CreateChildControls method! I do have that in my code, simply forgot to put it in the sample code above:

    // Create ChildControls // 
    protected override void CreateChildControls() 
    {
    ...
    this.ChildControlsCreated = true;
    }

    The real question would be:

    "What I think I need is to move the part of the code where the image url is being set (ref: SetImage) to some overridden method which will get called after control state has been restored."

    Wednesday, January 18, 2012 9:26 PM
  • User3866881 posted

    move the part of the code where the image url is being set (ref: SetImage) to some overridden method which will get called after control state has been restored."

    Move which codes?You mean the codes of CreateChildrenControl?

    And if you want to avoid the values from being lost,you should save the value in advance——such as in the ViewState……

    Reguards!

    Thursday, January 19, 2012 2:06 AM
  • User-1387801607 posted

    I need to move the piece of code ‘where the image URL is being set’:

    // Set Image Initial Url (ref: SetImage) // 
        string sURL = ThemePath + ImageUlrMap[SwitchValue]; 
        imgMain.ImageUrl = sURL; 

    The above code requires control-state to be restored. Which is not at the  time the CreateChildControls method is running.

    ViewState is not what I am looking for. I need to deal with control-state as control-state contains the new / latest value of the hidden field. Please reread what I described in my earlier posts.

    The working of the control is as follows:
    On a page the CompositeControl is displayed as an image.
    Each time the user clicks on the image a different image is being displayed (client side).
    The url’s of all possible images are stored in an array (client side).
    After each click the array-index of the current url is being stored in a hidden field (client side).
    After a postback the value of this hidden field is what is being used by server side code (through propety SwitchValue).

    Best reguards!

    Thursday, January 19, 2012 2:22 AM
  • User-1331152188 posted

     

    Based on asp.net lifecycle, dynamically added controls must be programmatically added to the Web page on each and every page visit. The best
    time to add these controls is during the initialization stage of the page life cycle, which occurs before the load view state stage.
    That is, we want to have the control hierarchy complete before the load view state stage arrives. For this reason, it is best to create an event handler for
    the Page Class's Init event in your code-behind class, and add your dynamic controls there.

     

    ASP.NET Page Life Cycle Overview

    http://msdn.microsoft.com/en-us/library/ms178472.aspx

    ASP.NET View State Overview

    http://msdn.microsoft.com/en-us/library/bb386448.aspx

    The ASP.NET View State

    http://msdn.microsoft.com/en-us/magazine/cc188774.aspx

    Sunday, January 22, 2012 7:33 PM
  • User-1387801607 posted

    Hi Anik SH, thanks for your input about the page life cycle.
    I will keep your suggestion about creating controls in the initialization stage in mind when adjusting our site.

    But what I am actually looking for is this:
    Inside my CompositeControl I only have the method CreateChildControls. During the execution of this method control state has not yet been restored.
    I am looking for an event/method inside the CompositeControl which gets called after control state has been restored.

    This is unrelated to view state.

    Best regards

    Tuesday, January 24, 2012 2:57 AM
  • User-1331152188 posted

    If your CompositeControl doesn't have other event/method by default, you need to to add it manually.

    Based on page life cycle, During load, if the current request is a postback control properties are loaded with information recovered from view state and control state. the method/event should belong to Postback event handling or Rendering stage.

    Tuesday, January 31, 2012 12:23 AM