Problem when trying to programmatically update an updatepanel

Last post 01-04-2008 4:06 AM by pichto. 4 replies.

Sort Posts:

  • Problem when trying to programmatically update an updatepanel

    12-29-2007, 7:36 PM
    • Loading...
    • pichto
    • Joined on 02-24-2003, 10:15 PM
    • Switzerland
    • Posts 25

     Hi all,

     

    I've tried numerous ways but still nothing so i hope somebody in this forum could point me in the right direction...

     

    I want to update the content of an updatepanel using the Update() method.

     

    My ASPX code is like this :


    1    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    2    <%@ Register Assembly="FUA" Namespace="Subgurim.Controles" TagPrefix="cc1" %>
    3    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4    <html xmlns="http://www.w3.org/1999/xhtml">
    5    <head runat="server">
    6        <title>Mon test à moi!!</title>
    7    </head>
    8    <body>
    9        <form id="form1" runat="server">
    10       <asp:ScriptManager ID="ScriptManager1" runat="server"
    11            EnablePartialRendering="true">
    12       </asp:ScriptManager>
    13       <div>
    14           <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" RenderMode="Block" >
    15           </asp:UpdatePanel> 
    16           <cc1:FileUploaderAJAX ID="FileUploaderAJAX1" runat="server" 
    17                       text_Add="Ajouter" 
    18                       text_Delete="Effacer" 
    19                       text_Uploading="Chargement en cours" />
    20       </div>
    21       </form>
    22   </body>
    23   </html>
    

     

    My CS code is like this :

     

    1    using System;
    2    using System.Data;
    3    using System.Configuration;
    4    using System.Linq;
    5    using System.Web;
    6    using System.Web.Security;
    7    using System.Web.UI;
    8    using System.Web.UI.WebControls;
    9    using System.Web.UI.WebControls.WebParts;
    10   using System.Web.UI.HtmlControls;
    11   using System.Xml.Linq;
    12   using Subgurim.Controles;
    13   using System.IO;
    14   
    15   public partial class _Default : System.Web.UI.Page 
    16   {
    17       private String ImageFolder = "~/temp";
    18       private Int32 ImgPerRow = 8;
    19   
    20       protected void Page_Load(object sender, EventArgs e)
    21       {
    22           ScriptManager1.RegisterAsyncPostBackControl(FileUploaderAJAX1);
    23   
    24           if (FileUploaderAJAX1.IsPosting)
    25               this.managePost();
    26   
    27           if (!Page.IsPostBack)
    28           {
    29               GenerateImagesTable();
    30           }
    31       }
    32   
    33       private void managePost()
    34       {
    35           //Response.Write("test");
    36   
    37           HttpPostedFileAJAX pf = FileUploaderAJAX1.PostedFile;
    38   
    39           if ((pf.ContentType.Equals("image/gif") || pf.ContentType.Equals("image/jpeg") || pf.ContentType.Equals("image/pjpeg")) && pf.ContentLength <= 100 * 1024)
    40               FileUploaderAJAX1.SaveAs("~/temp", pf.FileName);
    41   
    42           
    43           GenerateImagesTable();
    44   
    45           UpdatePanel1.Update();
    46       }
    47   
    48       private void GenerateImagesTable()
    49       {
    50           DirectoryInfo dir = new DirectoryInfo(MapPath(ImageFolder));
    51           FileInfo[] files = dir.GetFiles();
    52   
    53           //UpdatePanel1.Controls.Clear();
    54   
    55           UpdatePanel1.ContentTemplateContainer.Controls.Clear();
    56           //PlaceHolder1.Controls.Clear();
    57   
    58           if (files.Length == 0)
    59           {
    60               UpdatePanel1.ContentTemplateContainer.Controls.Add(new LiteralControl("Pas de fichiers présents dans ce dossier"));
    61   
    62               return;
    63           }
    64   
    65           Table ImgTable = new Table();
    66           TableRow ImgTableRow = new TableRow();
    67           TableCell ImgTableCell = new TableCell();
    68   
    69           UpdatePanel1.ContentTemplateContainer.Controls.Add(ImgTable);
    70   
    71           ImgTable.Rows.Add(ImgTableRow);
    72   
    73           ImgTable.Width = Unit.Percentage(100);
    74   
    75           Int32 i = 0;
    76           foreach (FileInfo file in files)
    77           {
    78               String FileName = file.Name;
    79               if (i++ == ImgPerRow)
    80               {
    81                   ImgTableRow = new TableRow();
    82                   ImgTable.Rows.Add(ImgTableRow);
    83   
    84                   i = 1;
    85               }
    86   
    87               ImgTableCell = new TableCell();
    88               ImgTableRow.Cells.Add(ImgTableCell);
    89   
    90               ImgTableCell.HorizontalAlign = HorizontalAlign.Center;
    91               ImgTableCell.Width = Unit.Percentage(12.5);
    92   
    93               Image myImage = new Image();
    94               myImage.ImageUrl = ImageFolder + "/" + FileName;
    95               myImage.Width = Unit.Pixel(50);
    96               myImage.Height = Unit.Pixel(50);
    97   
    98               ImgTableCell.Controls.Add(myImage);
    99           }
    100      }
    101  }
    

     

    In debug mode, i've put a breakpoint on line 45 in my CS code so i know that UpdatePanel1.Update() fires but i don't see any result on the page : no change!!!

     

    Could somebody help me?

     

    thanks,

    Pichto
     

    Pichto
  • Re: Problem when trying to programmatically update an updatepanel

    12-29-2007, 11:51 PM

     I dont understand what u r saying. when i saw ur code, u do not have anything inside the updatepanel1 (imean inside updatepanel1's content panel). so what r u expecting to get updated ?

    Vikram
    www.vikramlakhotia.com
    justlikethat.vikramlakhotia

    Please mark the answer if it helped you
  • Re: Problem when trying to programmatically update an updatepanel

    12-30-2007, 5:50 AM
    • Loading...
    • pichto
    • Joined on 02-24-2003, 10:15 PM
    • Switzerland
    • Posts 25

     Hi vik2000in,

     

    Eveything i have in my updatepanel is added programmatically (populate through all pictures in a specified directory and display them in the updatepanel).

    The idea is to upload pictures in a directory and, once uploaded, the updatepanel that displays the pictures gets updated to reflect the change (a new picture is in the directory).

    I want to do this programmatically as this will be some kind of a proof of concept for something more complex (an ajax file manager or approaching).

     Get the idea?
     

    Thanks,

    Pichto 

    Pichto
  • Re: Problem when trying to programmatically update an updatepanel

    01-04-2008, 3:40 AM
    Answer

    Hi,

    Thank you for your post!

    I don't think you can do it in this way.

    Please check http://blogs.infragistics.com/blogs/tony_lombardo/archive/2007/04/09/file-uploads-where-s-the-ajax.aspx to get an idea about how AJAX-FileUpload contorl works.

    Almost all of AJAX-FileUpload contorl use a hidden IFrame contorl, then, use some javascript code like this:

    <script type="text/javascript">
         function submitForm(frameName,upload){
         document.forms[0].action="default.aspx"
         document.forms[0].target=frameName;
         window.setTimeout(function(){
              var uploadE=document.getElementById(upload);
              uploadE.parentElement.appendChild(document.createTextNode(uploadE.value));
              uploadE.parentElement.replaceChild(uploadE.cloneNode(true),uploadE);
         },100);
         document.forms[0].submit();
    }
    </script>

     Then, you know, the "postbacked page" is not the main page, it is the page in Ifrmae window. So you can't see the change on yhe main page.

    I don't know wether this third-party upload-control give out some callback to the main page or not. If there is a callback to the main page when the upload request of the Iframe window is returned, you can update the updatepanel of the main page via javascript on the callback event.

    If you have further questions,let me know.

    Best Regards,

    Sincerely,
    Jin-Yu Yin
    Microsoft Online Community Support
  • Re: Problem when trying to programmatically update an updatepanel

    01-04-2008, 4:06 AM
    • Loading...
    • pichto
    • Joined on 02-24-2003, 10:15 PM
    • Switzerland
    • Posts 25

     Hi Jin-Yu Yin,

     You are right, my problem came from the IFRAME. As the uploader was in an iframe, i coudn't update the main window automatically.

     
    The FileuploaderAJAX found on the website (http://en.fileuploadajax.subgurim.net/) has the hability to perform custom javascript actions on demand. That was the solution !


    So, my solution is : doing a normal upload with this control and then, once uploaded, doing the refresh of the updatepanel with javascript (with a hidden LinkButton).

     

    In detail :

    Add the custom JS event to the uploader in the Page_Load:
     

    if (!Page.IsPostBack)
    {
    FileUploaderAJAX1.addCustomJS(FileUploaderAJAX.customJSevent.postUpload, "parent.RefreshPanel();");
    }


    Add the hidden linkbutton with javascript refresher on the page:

    <asp:LinkButton ID="FakeRefreshButton" runat="server" OnClick="FakeRefreshButton_Click"
                Style="display: none">LinkButton</asp:LinkButton>

    <script type="text/javascript">
    function RefreshPanel()
    {
    __doPostBack("FakeRefreshButton", "");
    }
    </script>

    Mark that button as a trigger for the updatepanel :

    <Triggers>
          <asp:AsyncPostBackTrigger ControlID="FakeRefreshButton" EventName="Click" />
    </Triggers>

     
    And finally add the code-behind code to handle the click event :

    protected void FakeRefreshButton_Click(object sender, EventArgs e)
    {
        PopulateImageFolder();
        //Reset fileuploader
        FileUploaderAJAX1.Reset();
    }
     
     HTH
    Pichto
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter