ASP.Net: User controls added to placeholder dynamically cannot retrieve values

前端 未结 8 1744
攒了一身酷
攒了一身酷 2021-02-09 02:53

I am adding some user controls dynamically to a PlaceHolder server control. My user control consists of some labels and some textbox controls.

When I submit the form

相关标签:
8条回答
  • 2021-02-09 03:47

    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)
  • 2021-02-09 03:55

    Ensure you are defining your dynamic controls at the class level and adding them to the ASP container:

    Private dynControl As ASP.MyNamespace_MyControl_ascx
    

    And when you instantiate the control, ensure you call LoadControl so the object is added properly:

    dynControl = CType(LoadControl("~/MyNamespace/MyControl/MyControl.ascx"), ASP.MyNamespace_MyControl_ascx)
    
    0 讨论(0)
提交回复
热议问题