Search code examples
c#asp.netimageupdatepanelasyncfileupload

Updating an Image url in an UpdatePanel in asp.net


I have a GridView in which I have an UpdatePanel. I am trying to update the I*mage URL* of my asp:image by using AJAX ASyncFileUpload. I've been searching on Google for a solution for the past 2 days now.

Here is the code:

Update panel:

<asp:UpdatePanel ID="pnlInfo" runat="server" Font-Names="Times New Roman" UpdateMode="Always" EnableViewState="true" 
                                style="display:none; background-color:#FFFFFF; padding:20px; margin:50px; border:3px solid #4B0303; color:Black; 
                                width:inherit;" 
                                >
                                <ContentTemplate>
                                <div runat="server" class="divTable" style="width:inherit;">

                                    <div runat="server" class="divRow" style="text-align:center; width:300px; float:left;">
                                        <asp:Image ID="imgUser" runat="server" ImageAlign="Middle" ImageUrl="~/silhouette.jpg"
                                             Width="100px" Height="100px" EnableViewState="true"/>
                                        <asp:ModalPopupExtender ID="ModalPopupEUpload" runat="server" 
                                            CancelControlID="btnClosePnl" OnCancelScript="HideModalPopup()"
                                            TargetControlID="imgUser" PopupControlID="pnlUploadImage" Drag="True" 
                                            BackgroundCssClass="ModalPopupBg" DynamicServicePath="" Enabled="True"  />
                                        <asp:Panel ID="pnlUploadImage" runat="server" Font-Names="Times New Roman" 
                                            style="display:none; background-color:#FFFFFF; padding:20px; 
                                            margin:50px; border:3px solid #4B0303; color:Black; width:inherit;" 
                                            >
                                                <asp:AsyncFileUpload ID="AsyncFileUpload" runat="server" OnUploadedComplete="OnUpdateComplete"/>
                                                <asp:Button ID="btnClosePnl" runat="server" Text="Close"/>
                                                <asp:Button ID="btnUpdate" runat="server" OnClick="OnUpdateComplete" Visible="false"  />
                                        </asp:Panel></div>

//more code
</div>
                                </ContentTemplate>
                                <Triggers>
                                    <asp:AsyncPostBackTrigger ControlID="AsyncFileUpload" EventName="UploadedComplete" />
                                </Triggers>
                            </asp:UpdatePanel></div>

The Update Panel is part of a ModalPopupExtender, so is the pnlUploadImage. These works fine. The GridView works fine too.

Background code:

protected void OnUpdateComplete(object sender, EventArgs e)
        {
            Image ImgUser = new Image();
            AsyncFileUpload asyncSender = new AsyncFileUpload();
            AsyncFileUpload asyncGv = new AsyncFileUpload();

            //finding the row of the sender
            asyncSender = (AsyncFileUpload)sender;

            foreach (GridViewRow Row in gvData.Rows)
            {
                asyncGv = (AsyncFileUpload)Row.Cells[0].FindControl("AsyncFileUpload");
                if (asyncSender.ClientID == asyncGv.ClientID)
                {
                    ImgUser = (Image)Row.Cells[0].FindControl("imgUser");
                    ImgUser.ImageUrl = asyncSender.FileName;
                    break;
                }
            }
        }

The btnUpdate was a possible solution that ended up not working. Basically, when I click on the Image, the AsyncFileUpload pops in a popup format. Then I choose an image and Upload it. I trigger the Update on the UploadComplete which is suppose to change the Image URL.

The problem is that the image that I see (silhouette.jpg) stays there even after going trough UploadComplete. When I do a step by step, I see my that the ImageURL is being changed. I checked and it is the right image that is being selected in the function.

Overall, why does, even thought the ImageURL is being changed in the backcode, the image that I see on my browser does not change.


Solution

  • I had to create an asp:button with a method click that had a javascript postback in it. Then I created a asynchPostBack trigger in teh update panel on the button I just created. It forces a postback but it is the only way to automatically update the picture.