文件上传后更新页面的问题

发布于 2024-10-14 04:25:37 字数 1818 浏览 5 评论 0原文

我遇到一个与 AsyncFileUpload 控件相关的奇怪问题。上传后,我通过从 ClientUploadComplete 事件处理程序调用 __doPostBack 函数来更新页面。第一次工作正常,但下次我尝试上传文件时,它会在上传之前先刷新页面,然后再次上传并刷新页面。不知道为什么刷新页面在上传前和上传后被调用两次。我有此代码的简化版本,其中存在此​​错误。有什么线索请问为什么会发生这种情况吗?

标记:

    <form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnClientUploadComplete="AsyncFileUpload1_ClientUploadComplete"
                OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />                
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="Button1" runat="server" Text="Refresh Data" OnClick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" EnableViewState="false"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>
</form>

Javascript:

    <script type="text/javascript">
    function AsyncFileUpload1_ClientUploadComplete() {
        var btnRefreshData = $get("<%=Button1.ClientID%>").name;
        __doPostBack(btnRefreshData, "");
    }

</script>

代码隐藏:

        protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Upload complete";
    }

    protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        System.Threading.Thread.Sleep(3000);
    }

I am having a strange issue associated with AsyncFileUpload control. after the upload, I am updating the page by calling__doPostBack function from ClientUploadComplete event handler. it works fine first time, but next time I try to upload the file, it refreshes the page first before uploading, then does the upload and refreshes the page again. not sure why refresh page is being called twice once before the upload and once after the upload. I have a simplified version of this code which has this bug. any clues please why it is happening?

Markup:

    <form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnClientUploadComplete="AsyncFileUpload1_ClientUploadComplete"
                OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />                
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="Button1" runat="server" Text="Refresh Data" OnClick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" EnableViewState="false"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>
</form>

Javascript:

    <script type="text/javascript">
    function AsyncFileUpload1_ClientUploadComplete() {
        var btnRefreshData = $get("<%=Button1.ClientID%>").name;
        __doPostBack(btnRefreshData, "");
    }

</script>

Code-Behind:

        protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Upload complete";
    }

    protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        System.Threading.Thread.Sleep(3000);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小鸟爱天空丶 2024-10-21 04:25:37

貌似没有人能回答这个问题。我仍然没有弄清楚为什么会发生这种情况,但我采取了一种解决方法,方法是在上传完成时在会话中添加一个标志,并在刷新页面上的数据之前检查该标志。这样在上传之前就不会发生数据刷新。谢谢。

looks like no one can answer this question. I still haven't figured out why this happens, but I put a workaround by adding a flag in session when the upload is complete and check that flag before refreshing the data on the page. this way the data refresh won't happen before the upload. thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文