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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2264541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:51:48  来源:igfitidea点击:

FileUpload and UpdatePanel: ScriptManager.RegisterPostBackControl works the second time

c#asp.net-ajaxfile-uploadupdatepanel

提问by VansFannel

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

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

I have an ASPX page with two UpdatePanels, one on the left that holds a TreeView and other on the right where I load dynamically user controls.

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

One user control, that I used on right panel, has a FileUpload control and a button to save that file on server. The ascx code to save control is:

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

<asp:UpdatePanel ID="UpdatePanelBotons" runat="server" RenderMode="Inline" 
    UpdateMode="Conditional">
<ContentTemplate>
    <asp:Button ID="Save" runat="server" Text="Guardar" 
                onclick="Save_Click" CssClass="button" />
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="Save" />
    </Triggers>
</asp:UpdatePanel>

I make a full postback to upload the file to the server and save it to database. But I always getting False on FileUpload.HasFile.

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

I problem is the right UpdatePanel. I need it to load dynamically the user controls. This panel has three UpdatePanels to load the three user controls that I use.

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

Maybe I can use an Async File Uploader or delete the right Update Panel and do a full postback to load controls dynamically.

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

Any advice?
UPDATE:

有什么建议吗?
更新:

RegisterPostBackControlworks... the second time I click on save button. First time FileUpload.HasFile is FALSE, and second time is TRUE.

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

Second Update
On first click I also check ScriptManager.IsInAsyncPostBack and is FALSE. I don't understand ANYTHING!!

第二次更新
在第一次单击时,我还检查了 ScriptManager.IsInAsyncPostBack 并且是 FALSE。我什么都不懂!!

Why?

为什么?

The code to load user control first time, and on each postback is:

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

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);
}

Thank you.

谢谢你。

采纳答案by user326796

For me this solution worked:

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

add Page.Form.Attributes.Add("enctype", "multipart/form-data");

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

The enctype attribute is missing on the first postback.

第一次回发时缺少 enctype 属性。

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

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

回答by Bhaskar

Fill Upload requires a full post, and if you use a Update Panel, it does a partial post back. So, FileUpload by itself will not work. You will have to handle it differently by calling the RegisterPostBackControl.

填充上传需要完整的帖子,如果您使用更新面板,它会回发部分内容。所以,FileUpload 本身是行不通的。您将不得不通过调用RegisterPostBackControl.

See this link for details --> http://geekswithblogs.net/mmintoff/archive/2009/04/01/fileupload-within-updatepanel.aspx

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

回答by emmilely

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.

我遇到了这个确切的问题,我相信这与您在 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.

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

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

我使用 XSL 将我的 XML 转换为包含 ASP.NET 控件的动态页面,因此基本上我执行了以下操作:

<div class="hide">
    <asp:FileUpload CssClass="upload" identity="addVersion_{@id}_{count(draft) + 1}" runat="server" />
    <asp:Button Text="Add File" identity="addVersionBtn_{@id}_{count(draft) + 1}" fileControlIdentity="addVersion_{@id}_{count(draft) + 1}" action="addVersion" runat="server" />
</div>


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 :)

我删除了很多代码,但它仍然应该得到大致的概念:)

回答by RepDetec

Put the following in your form tag:

将以下内容放入您的表单标签中:

enctype="multipart/form-data"

enctype="multipart/form-data"

回答by David

I had to combine both advices here. I am using an UpdatePanel to dynamically load an UserControl and the FileUpload is in the UserControl. I had to:

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

  • RegisterPostBackControl in the user control's Page_Load

  • add enctype="multipart/form-data" to the form element of the page, which contains the update panel, the code Page.Form.Attributes.Add("enctype", "multipart/form-data"); didn't work for me, it had to be in the aspx

  • 用户控件的 Page_Load 中的 RegisterPostBackControl

  • 将enctype="multipart/form-data"添加到页面的form元素中,其中包含更新面板,代码Page.Form.Attributes.Add("enctype", "multipart/form-data"); 对我不起作用,它必须在 aspx 中

Hope this helps.

希望这可以帮助。

回答by Zocolo Fishing

It worked for me, only when I put it in the Page_Init

它对我有用,只有当我把它放在 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