ASP.Net UpdatePanel未更新,部分回发触发

时间:2013-04-02 20:37:59

标签: c# asp.net ajax updatepanel

所以我已经在这个问题上坚持了几个小时,搜索论坛,尝试了人们所建议的一切,但我无法让它发挥作用。

我有一个(相当大的)网络表单。表单上的某个地方是一个updatePanel,它包含一个CuteSoft Ajax Uploader控件。当文件上传时,该控件调用我的代码中的函数 - 这很好。在该函数中,我对updatePanel中的内容进行了一些更改(添加缩略图等),但更改从未反映在前端。

这是表格(不相关的位被删除):

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VehicleSubmissionWizard.ascx.cs"
    Inherits="MaxAdmin.UserControls.VehicleSubmissionWizard" %>
...
<div id="template3" style="display: none;">
    <div class="top-section">
        <div id="column-left">
            ...
        <div id="column-right">
            ...
    <form runat="server" id="form1" action="">
    <%--Panel 1--%>
    <asp:Panel runat="server" ID="pnlSubmissionWizard1">
        ...
    </asp:Panel>
    <%--/Panel 1--%>
    <%--Panel 2--%>
    <asp:Panel runat="server" ID="pnlSubmissionWizard2">
        ...
            <h2>
                Vehicle images</h2>
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"
                AjaxFrameworkMode="Enabled" ClientIDMode="Static" EnableViewState="true" LoadScriptsBeforeUI="true"
                ScriptMode="Auto" ViewStateMode="Enabled">
            </asp:ScriptManager>
            <asp:UpdatePanel runat="server" EnableViewState="false" ID="upImageUpload" UpdateMode="Conditional">
                <Triggers>
                    <%--                    <asp:AsyncPostBackTrigger ControlID="Uploader" 
                           EventName="FileUploaded" />
                    --%>
                </Triggers>
                <ContentTemplate>
                    <input id="hdnImages" class="imagesUpload" type="hidden" />
                    <asp:HiddenField ID="hdn" runat="server" />
                    <CuteWebUI:Uploader ID="Uploader" runat="server" EnableViewState="false" MaxFilesLimit="3"
                        MaxFilesLimitMsg="Please upload a maximum of 3 photos" InsertButtonStyle-Width="280px"
                        MultipleFilesUpload="true" InsertText="Upload Photos [Max 3 Photos at up to 4mb each]"
                        OnFileUploaded="Uploader_FileUploaded" OnUploadCompleted="Uploader_Complete">
                        <ValidateOption MaxSizeKB="4096" AllowedFileExtensions="jpg,gif,png" />
                    </CuteWebUI:Uploader>
                    <div id="attachmentsDiv">
                        <CuteWebUI:UploadAttachments EnableViewState="false" runat="server" ID="Attachments"
                            MaxFilesLimit="3" Visible="false">
                            <ValidateOption MaxSizeKB="4096" AllowedFileExtensions="jpg,gif,png" />
                        </CuteWebUI:UploadAttachments>
                    </div>
                    <br />
                    <asp:Panel ID="pnlImageDetails_1" runat="server" CssClass="display_none" Style="float: left;
                        margin-bottom: 0">
                        <div class="uploadedImage" style="clear: both; float: left; margin: 5px 20px 5px 75px;">
                            <div style="text-align: center">
                                <asp:Image ID="imgUploadedImage1" runat="server" />
                            </div>
                            <div style="text-align: center">
                                <asp:LinkButton ID="btnRemoveImage_1" runat="server" OnCommand="RemoveImage_Click"
                                    CommandName="1" Text="Remove Image" CssClass="RemoveLinkButton" Style="margin-left: 15px;" />
                            </div>
                        </div>
                    </asp:Panel>
                    <asp:Panel ID="pnlImageDetails_2" runat="server" CssClass="display_none" Style="float: left;">
                        <div class="uploadedImage" style="clear: both; float: left; margin: 5px 20px 5px 20px;">
                            <div style="text-align: center">
                                <asp:Image ID="imgUploadedImage2" runat="server" />
                            </div>
                            <div style="text-align: center">
                                <asp:LinkButton ID="btnRemoveImage_2" runat="server" OnCommand="RemoveImage_Click"
                                    CommandName="2" Text="Remove Image" CssClass="RemoveLinkButton" Style="margin-left: 15px;" />
                            </div>
                        </div>
                    </asp:Panel>
                    <asp:Panel ID="pnlImageDetails_3" runat="server" CssClass="display_none" Style="float: left;">
                        <div class="uploadedImage" style="clear: both; float: left; margin: 5px 20px 5px 20px;">
                            <div style="text-align: center">
                                <asp:Image ID="imgUploadedImage3" runat="server" />
                            </div>
                            <div style="text-align: center">
                                <asp:LinkButton ID="btnRemoveImage_3" runat="server" OnCommand="RemoveImage_Click"
                                    CommandName="3" Text="Remove Image" CssClass="RemoveLinkButton" Style="margin-left: 15px;" />
                            </div>
                        </div>
                    </asp:Panel>
                    <script language="javascript" type="text/javascript">
                        var Image_Count = $('#<%= hdn.ClientID %>').val(); 
                    </script>
                </ContentTemplate>
            </asp:UpdatePanel>
            ...
    </asp:Panel>
    <%-- / Panel 2--%>
    <%--Panel 3--%>
    <asp:Panel runat="server" ID="pnlSubmissionWizard3">
        ...
    </asp:Panel>
    <%-- / Panel 3--%>
    <%-- Panel 4--%>
    <asp:Panel Visible="false" runat="server" ID="pnlSubmissionWizard4">
        ...
    </asp:Panel>
    <%-- / Panel 4--%>
    </form>
    ...
</div>

Codebehind看起来像这样:

...

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //Uploader.FileUploaded += new CuteWebUI.UploaderEventHandler(Uploader_FileUploaded);
            Uploader.FileUploaded += Uploader_FileUploaded;
            Uploader.UploadCompleted += Uploader_Complete;
            Attachments.AttachmentRemoveClicked += Attachments_AttachmentRemoveClicked;
            Attachments.InsertButton.Style["display"] = "none";
            ((HiddenField)FindControl("hdn")).Value = "0";
        }

...

        protected void Uploader_FileUploaded(object sender, CuteWebUI.UploaderEventArgs args)
        {
            ...

                using (System.IO.Stream stream = args.OpenStream())
                {
                    // Check if file already exists - seems to do each image twice for some reason...
                    foreach (var ph in Photos)
                        if (String.Equals(ph.FileName, args.FileName))
                            return;

                    ...

                    // Show image thumbnail
                    ShowUploadedPhotoDetails(p, ImageCount);
                }
            }
            catch (Exception ex)
            {
               ...
            }
        }

    private void ShowUploadedPhotoDetails(Photo photo, int imageNo)
    {
        if (imageNo <= 3)
        {
            var imagePanel = (Panel)FindControl(String.Concat("pnlImageDetails_", imageNo));
            imagePanel.CssClass = "display_block";
            imagePanel.Visible = true;

            var uploadedImage = (System.Web.UI.WebControls.Image)FindControl(String.Concat("imgUploadedImage", imageNo));
            uploadedImage.ImageUrl = photo.ImageUrlThumb;
            uploadedImage.Visible = true;
            uploadedImage.CssClass = "CSS_CLASS";
        }
    }

        protected void Uploader_Complete(object sender, CuteWebUI.UploaderEventArgs[] args)
        {
            Uploader uploader = (Uploader)sender;
            uploader.InsertButton.Enabled = (Photos.Count < 3);

            // Force updatePanel to update
            upImageUpload.Update();
        }

因此,上传图像时上述两个功能都会触发。图像数据很好。调试分配给控件的值(例如:ImageUrl,CssClass等)表明它们已正确分配给控件。但是值永远不会在前端刷新。

我错过了哪些想法?

谢谢堆 格雷格

2 个答案:

答案 0 :(得分:0)

P,我明白了!!!基本上我重建了整个表格,一步一步繁琐乏味。虽然我不确切知道是什么阻止了它的工作,我怀疑它可能是由在表单其他部分的按钮上设置的以下属性引起的:

PostbackUrl = ".."

将其添加回我的工作表单会导致updatePanel中断..但是从原来的破碎表单中删除它并不能解决它,所以也许还有其他内容。

没有代码被更改,所以它与事件排序等无关(虽然TheGeekYouNeed是个好主意。

这是一个从Asp.Net 2.0更新到4.0的项目..也许升级中的某些东西搞砸了。谁知道,无论如何至少它是固定的。呼!

答案 1 :(得分:0)

如果UpdatePanel内部有任何错误,包括div标签的格式不正确等,则可能无法刷新面板。如果没有让UpdatePanel刷新,并且您尝试了所有常见的.Update()等操作,请检查UpdatePanel的内容是否存在问题,并且应该对其进行修复。