【问题标题】:Asp.NET Updatepanel not working as expectedAsp.NET Updatepanel 未按预期工作
【发布时间】:2013-08-21 14:50:42
【问题描述】:

这就是我想要的.. 我正在通过 FileUpload 控件上传图片。但是当我触发 updatepanel 时,Fileupload 控件显示它是空的。我不知道为什么。

这就是我需要的..

只有当我点击上传按钮时我才需要更新我的更新面板,然后我不想更新它的内容。我需要在我的编码中做哪些更改

这是aspx代码..

 <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"/>
    <div>


            <fieldset style="width:50%; margin-left:300px">
            <legend>Upload Files</legend>
                <asp:FileUpload runat="server" ID="UploadImages" style="background-color:white; position:relative; font-family:'Palatino Linotype'; font-size:medium" Width="500px" AllowMultiple="true"/>
                <asp:Button runat="server" ID="uploadedFile" style="position:relative;  font-family:'Palatino Linotype'; font-size:medium; width: 112px; height: 29px;" Text="Upload" OnClick="uploadFile_Click" />
            </fieldset>


    <div id="back" runat="server" class="transbox" style="width:100%;height:100%;left:0px;top:0px;position:absolute" Visible="false">
     <asp:UpdatePanel ID="updtpanel" runat="server" UpdateMode="Conditional" style="width:100%;height:100%;left:0px;top:0px;position:absolute">
           <ContentTemplate>
                <asp:Button ID="btnsave" runat="server" UseSubmitBehavior="true" Text="Find Images" OnClick="btnsave_Click" Font-Bold="true" style="position:absolute" BackColor="Yellow"></asp:Button>--%>
           </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="uploadedFile" EventName="Click"/>
            </Triggers>
        </asp:UpdatePanel>

    </div>

    </div>
    </form>

这里是 FileUpload 的 .cs 代码..

protected void uploadFile_Click(object sender, EventArgs e)
    {

        if (UploadImages.HasFiles)
        {

            string fileExt = Path.GetExtension(UploadImages.FileName).ToLower();
            if (fileExt == ".jpeg" || fileExt == ".png" || fileExt == ".jpg" || fileExt == ".bmp")
            {
                foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles)
                {
                    count1 += 1;
                }

                foreach(HttpPostedFile uploadedFile in UploadImages.PostedFiles)
                {
                    count += 1;

                    filepath = Server.MapPath("~/Images/" + uploadedFile.FileName);
                    uploadedFile.SaveAs(filepath);
                    newpath = "../Images/" + uploadedFile.FileName;
                    try
                    {
                        createImgPanel();
                        Image nimg = Page.FindControl("img" + count) as Image;
                        nimg.ImageUrl = newpath.ToString();

                        Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Files Uploaded!!');", true);

                    }

                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }

                }
            }
            else

            {
                Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Please Select only Image Files!!');", true);
            }

        }
        else
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Please Select a File First!!');", true);
        }
    }

如果我在 updatepanel uploadFile.HasImages 中评论触发器将起作用.. 但如果我取消评论它将直接登陆 请先选择一个文件 错误消息.. 在代码中犯了什么错误..

【问题讨论】:

    标签: c# html asp.net file-upload updatepanel


    【解决方案1】:

    您的updatepanel 在单击UploadFile 控件后立即触发回发,因此所选择的文件会在第一个请求时立即回发。当您通过单击按钮提交第二个请求时,UploadFile 控件再次为空。 UploadFile 控件(或任何input:file 元素,就此而言)不会在回发后保留所选值。您所看到的都是设计使然。

    最简单的做法是避免初始回发并完全在客户端进行验证。换句话说,从更新面板中删除triggers 部分,因为您已经发现它有效。显然,当单击“上传文件”按钮时,您仍然会在服务器端进行验证。

    还有其他替代方法,但底线是当您在页面中有 UploadFile 控件时,您无法执行完整的回发(这是 UpdaPanels 在幕后所做的),因为这将导致选择的文件提交。为了证明我的意思,请在Page_Load 上设置一个断点,并在更新面板触发更新时向Request.Files 添加一个监视。

    【讨论】:

    • @Icarus.. 但据我所知,触发器只允许在我的目标更新面板上进行更新,对吧?如果我想让我的更新面板只更新一次我需要使用什么方法..
    • @Kaushik UpdatePanels 将仅更新其内容,但会执行完整回发。如果你在Page_Load 上设置一个断点,你可以很容易地看到这一点,看看会发生什么。确保将手表添加到 Request.Files
    • @Icarus.. 谢谢你.. 这真的启发了我.. 好吧,我会根据你提供的知识来解决我的问题......谢谢.. :)
    猜你喜欢
    • 1970-01-01
    • 2010-11-10
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多