Avoiding postbacks

Last post 08-23-2010, 11:44 PM by cutechat. 7 replies.
Sort Posts: Previous Next
  •  08-12-2010, 1:10 AM 63347

    Avoiding postbacks

    Hi
     
    We're currently evaluating the ajaxuploader for our application.

    We want the uploader to do it's thing BUT not do a postback -- we'll then do an ajax postback so that we can update some info and put the file into a database without doing a postback.
     
     I've put in script as follows:-
     
         function CuteWebUI_AjaxUploader_OnProgress(enable, filename, begintime, uploadedsize, totalsize) {
             return false;
         }
     
     but what we're finding is that it still posts back.
     
    I've put in a lot of the javascript global functions and note that it seems to posted back after one time through Progress but that might just be a coincedence.
     
    I'm guessing I'm doing something wrong but cannot see why.
     
    My markup looks like:
     
     
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
    <%@ Register Assembly="CuteWebUI.AjaxUploader" Namespace="CuteWebUI" TagPrefix="CuteWebUI" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
        <script type="text/javascript">
          function pageLoad() {
          }
        
         function $() {
             var aElements = new Array();
             var nLength = arguments.length;
             for (var i = 0; i < nLength; i++) {
                 var oElement = arguments[i];
                 if (typeof oElement == 'string') {
                     oElement = document.getElementById(oElement);
                 }
                 if (nLength == 1) {
                     return oElement;
                 }
                 aElements.push(oElement);
             }
             return aElements;
         }

         function HideDiv(form_element) {
             if ($(form_element) == null) {
                 alert(form_element);
             }
             else {
                 $(form_element).style.display = 'none';
             }
         }

         function ShowDiv(form_element) {
             if ($(form_element) == null) {
                 alert(form_element);
             }
             else {
                 $(form_element).style.display = '';
             }
         }

         function CuteWebUI_AjaxUploader_OnStart() {
             alert('CuteWebUI_AjaxUploader_OnStart');
             
         }

         function CuteWebUI_AjaxUploader_OnStop() {
             alert('CuteWebUI_AjaxUploader_OnStop');
         }

         function CuteWebUI_AjaxUploader_OnQueueUI(list) {
             ShowDiv('CuteWebUI_AjaxUploader_OnQueueUI');
             return false;
         }

         function CuteWebUI_AjaxUploader_OnProgress(enable, filename, begintime, uploadedsize, totalsize) {
             ShowDiv('CuteWebUI_AjaxUploader_OnProgress');
             return false;
         }

         function CuteWebUI_AjaxUploader_OnTaskComplete(obj) {
             var sReturn = 'false';
             alert("Complete : " + obj.FileName + " : " + obj.FileGuid);
             try {
                 alert('put the call to ajax postback here!');
             }
             catch (e) {
                 alert(e);
                 alert('problem');
             }
         }

         function CuteWebUI_AjaxUploader_OnPostback() {
             alert('CuteWebUI_AjaxUploader_OnPostback');
             return false;
         }    
          
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <a href="#" id="btnUpload">Choose File</a>
            <a href="#" id="btnCancel">Cancel</a>
            <img id="ProgressBar" style="display:none;" src="images/smactivity.gif" alt="" />
            <CuteWebUI:Uploader runat="server"
                ID="Uploader1"
                CancelButtonID="btnCancel"
                InsertButtonID="btnUpload">
                <ValidateOption MaxSizeKB="100" AllowedFileExtensions="jpg,png,gif"/>
            </CuteWebUI:Uploader>
        </div>
        </form>
    </body>
    </html>
    My code behinds at this point doesnt do much apart from this
     
    public partial class Default3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Check if we get here
            }
        }
    }
     
    Any ideas?
     
    James Beauchamp
  •  08-15-2010, 10:00 PM 63410 in reply to 63347

    Re: Avoiding postbacks

    Hi,
     
       function CuteWebUI_AjaxUploader_OnPostback() {
             alert('CuteWebUI_AjaxUploader_OnPostback');
             return false;
         }  
     
    this code shall works.
     
    did you see that message?
     
    Regards,
    Terry
     
  •  08-16-2010, 3:57 PM 63429 in reply to 63410

    Re: Avoiding postbacks

    Hi

    Yes we saw that message and after it gets to it, it doesn't post back.

    _However_, before that it has already posted back and I don't understand why



    James
  •  08-17-2010, 1:47 AM 63437 in reply to 63429

    Re: Avoiding postbacks

    Terry
     
    I can send you my solution if that helps to see what I'm doing. Just need an email address or place to upload to?
     
    James
  •  08-18-2010, 2:26 AM 63468 in reply to 63347

    Re: Avoiding postbacks

    Hi,
     
    Uploader will postback it via XMLHttpRequest and fire the FileValidating event.
     
    so Page_Load will be called.
     
    Regards,
    Terry
  •  08-18-2010, 5:00 PM 63497 in reply to 63468

    Re: Avoiding postbacks

    Hi Terry,
     
    Okay -- just to confirm then...
     
    Even though I had no events in my markup and had the global javascript function returning FALSE for the postback it will still postback?
     
    There is no way to just have the httpmodule/handler do the upload and leave it at that?
     
    James
  •  08-18-2010, 5:35 PM 63498 in reply to 63497

    Re: Avoiding postbacks

    Hi Terry
     
    Tested a bit more and I can confirm that the OnFileValidating event is the one firing.
     
    However in my case I DONT have a OnFileValidating event and so don't want that postback to happen.
     
    I can see that CuteWebUI_AjaxUploader_OnSelect() gets called but returning false stops the entire select process and return will then just let it continue and then calls the OnFileValidating event.
     
    Is there any way we can stop the OnFileValidating postback/event happening?
     
    James
  •  08-23-2010, 11:44 PM 63579 in reply to 63498

    Re: Avoiding postbacks

    Hi,
     
    For security reason,
     
    Uploader must do that postback once to validate the uploaded file , via the uploader object in that page.
     
    So it's not just for FileValidating event.
     
    If you must doing something in Page_Load which is conflict with the uploader,
     
    I suggest you move the code to Page's LoadComplete event.
     
    Or you can use this code to determine whether the uploader is doing such postback:
     
       string validating;
       System.Collections.Specialized.NameValueCollection nvc;
       nvc=(System.Collections.Specialized.NameValueCollection)Context.Items["CuteWebUI.AjaxUploader.QueryString"];
       if(nvc!=null)
        validating=nvc["_UploadControlID"];
       else
        validating=Context.Request.QueryString["_UploadControlID"];
       if(!string.IsNullOrEmpty(validating))
           return;
       
    Regards,
    Terry
View as RSS news feed in XML