FileUpload和UpdatePanel:ScriptManager.RegisterPostBackControl第二次工作

我正在使用C#和Visual Studio 2008 SP1开发ASP.NET应用程序。 我正在使用WebForms。

我有一个带有两个UpdatePanel的ASPX页面,一个在左边,它包含一个TreeView,另一个在右边,我动态加载用户控件。

我在右侧面板上使用的一个用户控件具有FileUpload控件和用于将该文件保存在服务器上的按钮。 保存控件的ascx代码是:

        

我做了一个完整的回发,将文件上传到服务器并将其保存到数据库。 但我总是在FileUpload.HasFile上得到False。

我的问题是正确的UpdatePanel。 我需要它动态加载用户控件。 此面板有三个UpdatePanel来加载我使用的三个用户控件。

也许我可以使用异步文件上传器或删除正确的更新面板并执行完整回发以动态加载控件。

任何建议?
更新:

RegisterPostBackControl 工作 ……我第二次点击保存按钮。 第一次FileUpload.HasFile为FALSE,第二次为TRUE。

第二次更新
在第一次单击时,我还检查ScriptManager.IsInAsyncPostBack并且为FALSE。 我不明白任何事!

为什么?

第一次加载用户控件的代码,以及每次回发的代码是:

 DynamicControls.CreateDestination ud = this.LoadControl(ucUrl) as DynamicControls.CreateDestination; if (ud != null) { Button save = ud.FindControl("Save") as Button; if (save != null) ScriptManager1.RegisterPostBackControl(save); PanelDestination.Controls.Add(ud); } 

谢谢。

对我来说这个解决方案有效

添加Page.Form.Attributes.Add(“enctype”,“multipart / form-data”);

第一个回发中缺少enctype属性。

http://adamnoffie.blogspot.com/2007/10/file-upload-not-working-on-first.html

将以下内容放在表单标记中:

ENCTYPE =“多部分/格式数据”

填充上传需要完整的post,如果您使用更新面板,它会进行部分回发。 因此,FileUpload本身不起作用。 您必须通过调用RegisterPostBackControl以不同方式处理它。

有关详细信息,请参阅此链接 – > http://geekswithblogs.net/mmintoff/archive/2009/04/01/fileupload-within-updatepanel.aspx

为了回应您的第二次更新,

我遇到了这个问题,我认为这与你在UpdatePanel内部为页面添加动态控件这一事实有关。

可能有更好的方法在那里做,因为我当然不是ASP.NET专家,但我能够通过在使用CSS隐藏的div中添加控件来自己解决这个问题-before-需要,然后自动递增“虚拟”控件的ID,这样就没有冲突,我可以继续添加任意多个。 这样,当UpdatePanel被触发时,它完全识别控件及其内容,当然除了隐藏的控件。

我正在使用XSL将我的XML转换为包含ASP.NET控件的动态页面,所以基本上我做了以下操作:

 

然后在代码隐藏中:

 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 

我删除了很多代码,但它仍然应该得到一般的想法:)

我必须在这里结合两个建议。 我使用UpdatePanel动态加载UserControl,FileUpload在UserControl中。 我不得不:

  • RegisterPostBackControl在用户控件的Page_Load中

  • 将enctype =“multipart / form-data”添加到页面的表单元素,其中包含更新面板,代码Page.Form.Attributes.Add(“enctype”,“multipart / form-data”); 不适合我,它必须在aspx中

希望这可以帮助。

只有当我把它放在Page_Init中时,它才对我有用

 Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init Page.Form.Attributes.Add("enctype", "multipart/form-data") End Sub