FileUpload and UpdatePanel: ScriptManager.RegisterPostBackControl works the second time

前端 未结 6 1821
你的背包
你的背包 2020-12-16 23:19

I\'m developing an ASP.NET application with C# and Visual Studio 2008 SP1. I\'m using WebForms.

I have an ASPX page with two UpdatePanels, one on the left that holds

6条回答
  •  心在旅途
    2020-12-16 23:23

    In response to your second update,

    I had this exact problem, and I believe it has to do with the fact that you are adding a dynamic control to your page inside of the UpdatePanel.

    There may be a better way to do it out there, as I am certainly no ASP.NET expert, but I was able to solve this problem on my own by adding the control in a div that was hidden using CSS -before- it was needed, and then auto-incrementing the "dummy" control's ID so there are no conflicts, and I can keep adding as many as I want. That way, when the UpdatePanel is triggered it fully recognizes the controls and their contents, excepting the hidden ones of course.

    I am using XSL to transform my XML into a dynamic page that includes ASP.NET controls, so essentially I did the following:


    And then in the code-behind:

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        //Code to generate the data (stripped out because it is generated in a different manner than the original poster)
    
        //Add events for all of the new-found controls depending on their type
        recursiveAddEvents(nameOfPlaceHolder.Controls)
    End Sub
    
    
    //Add events for all of the new-found controls depending on their type
    Sub recursiveAddEvents(ByRef controls As ControlCollection)
        For Each con As Control In controls
            If con.Controls.Count > 0 Then
                recursiveAddEvents(con.Controls)
            End If
    
            //Try to cast the control to different data types
            Dim btn As Button = TryCast(con, Button)
            Dim file As FileUpload = TryCast(con, FileUpload)
    
            //Test to see which type the control was and apply the actions to it
            If Not btn Is Nothing Then
                //Assign the correct click events
                If btn.Attributes.Item("action") = "addVersion" Then
                    AddHandler btn.Click, AddressOf addDraftVersion
                    btn.ID = btn.Attributes.Item("identity")
    
                    //Register the control with the ScriptManager
                    ScriptManager.GetCurrent(Page).RegisterPostBackControl(btn)
                End If
            ElseIf Not file Is Nothing Then
                //Assign the correct click events
                file.ID = file.Attributes.Item("identity")
            End If
        Next
    End Sub
    
    Protected Sub addDraftVersion(ByVal sender As Button, ByVal e As EventArgs)
        Dim fileName as String = sender.Attributes("title").Replace(" ", "_") & "_D" & info("draftID") & "_V" & info("versionID")
        Dim inputControl As FileUpload = TryCast(trackPH.FindControl(sender.Attributes("fileControlIdentity")), FileUpload)
    
        If inputControl Is Nothing Then
            Exit Sub
        End If
    
        //Do whatever need to be done
    
        //Trigger UpdatePanel(s)
        nameOfUpdatePanel.Update()
    End Sub
    

    I stripped out a lot of the code, but it should still get the general idea across :)

提交回复
热议问题