Problem with dynamic controls in .NET

前端 未结 9 1122
迷失自我
迷失自我 2020-12-13 07:50

Problem with dynamic controls

Hello all,

I\'m wanting to create some dynamic controls, and have them persist their viewstate across page loads. Easy enough,

相关标签:
9条回答
  • 2020-12-13 07:57

    ViewState-backed properties are only persisted to ViewState if the control is currently tracking ViewState. This is by design to keep ViewState as small as possible: it should only contain data that is truly dynamic. The upshot of this is that:

    ViewState propeties set during the Init event are not backed to ViewState (because the Page has not yet started tracking ViewState). Thus Init is a good place to add controls and set (a) properties that won't change between postbacks (ID, CssClass...) as well as initial values for dynamic properties (which can then be modified by code in the rest of the page lifecycle - Load, event handlers, PreRender).

    When dynamically adding controls in Load or PreRender, ViewState is being tracked. The developer can then control which propeties are persisted for dynamically added controls as follows:

    • Properties set before the control is added to the page's control tree are not persisted to ViewState. You typically set properties that are not dynamic (ID etc) before adding a control to the control tree.

    • Properties set after the control is added to the page's control tree are persisted to ViewState (ViewState tracking is enabled from before the Load Event to after the PreRender event).

    In your case, your PreRender handler is setting properties before adding the control to the page's control tree. To get the result you want, set dynamic properties after adding the control to the control tree: .

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        ValueLinkButton tempLink = new ValueLinkButton(); // [CASE 2]        
        tempLink.ID = "valueLinkButton"; // Not persisted to ViewState
        Controls.Clear();
        Controls.Add(tempLink);
        tempLink.Value = "new value";  // Persisted to ViewState
        tempLink.Text = "Click";       // Persisted to ViewState
    }
    
    0 讨论(0)
  • 2020-12-13 07:57

    Try calling Page.RegisterRequiresControlState(). You can also use RequiresControlState() to check if it's already been registered.

    0 讨论(0)
  • 2020-12-13 08:00

    As others have statement you'll need to ensure that you are creating via the Init method. To learn more about the ASP.NET page life cycle check out this article: http://msdn.microsoft.com/en-us/library/ms178472.aspx

    0 讨论(0)
  • 2020-12-13 08:01

    Thank you for your help, but I tried that and it didn't make a difference. Besides, OnLoad works just as well for dynamic controls as OnInit, as long as you give your controls the same IDs every time.

    0 讨论(0)
  • 2020-12-13 08:03

    Control added before SaveViewState method called in control life cycle should persist their values. I would concur with Joe's answer. Check this image

    http://emanish.googlepages.com/Asp.Net2.0Lifecycle.PNG

    0 讨论(0)
  • 2020-12-13 08:04

    I figured out yesterday that you can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.

    Protected Overrides Sub LoadViewState(ByVal savedState As Object)
        MyBase.LoadViewState(savedState)
        If IsPostBack Then
            CreateMyControls()
        End If
    End Sub
    
    0 讨论(0)
提交回复
热议问题