2011-11-14 24 views
0

在一個奇怪的情況下我的page_Load運行兩次,所有代碼都在page_Load中。
我不能使用Page.IsPostBac,因爲在Page_Loads中都是假的。
但什麼是奇怪的情況?
我在我的項目中有一個下載文件的頁面,
當你點擊下載鏈接之一(我使用錨點,所以IsPostBack爲false)page_Load運行,並在頁面加載我檢查DownloadPath Querystring。
如果DownloadPath不爲空,則通過傳遞用於向我的用戶顯示下載窗口的DownloadPath來跳轉到Handler.ashx。
在這個過程中,我在我的數據庫中有一些日誌,並因爲這兩次運行它們將重複。
但是當兩次運行accours?
當您嘗試通過網絡下載管理器下載鏈接時,page_Load會運行兩次!如何預防兩次Page_Load正在運行 - Page.IsPostBack始終爲假

我該如何防止page_Load的第二次運行或者有沒有辦法識別它?

編輯:
我的Page_Load:

namespace NiceFileExplorer.en 
{ 
    public partial class Download : System.Web.UI.Page 
    { 
     public string Folders = ""; 
     public string Files = ""; 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      Statistics_body_inside.DataBind(); 

      if (Request.QueryString["Path"] != null) 
      { 
       if (Request.QueryString["Path"] == "..") 
       { 
        ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "YouCanNotDoThatAnyMore", "YouCanNotDoThatAnyMore();", true); 
       } 
       else 
       { 
        MainCodes(); 
       } 
      } 
      else 
      { 
       MainCodes(); 
      } 
     } 

     private void MainCodes() 
     { 
      if (Request.QueryString["DownloadPath"] != null) 
      { 
       string DownloadPath = Request.QueryString["DownloadPath"].ToString(); 
       string FilePath = "C:" + DownloadPath.Replace(@"/", @"\\"); 

       if (Session["User_ID"] != null) 
       { 
        string FileName = Request.QueryString["FileName"].ToString(); 
        string FileSize = Request.QueryString["FileSize"].ToString(); 
        string FileCreationDate = Request.QueryString["FileCreationDate"].ToString(); 

        int Downloaded_Count_4Today = DataLayer.Download.Count_By_UserID_Today(int.Parse(HttpContext.Current.Session["User_ID"].ToString()), DateTime.Now); 
        if (Downloaded_Count_4Today <= 10) 
        { 
         DataSet dsDownload = DataLayer.Download.Size_By_UserID_Today(int.Parse(HttpContext.Current.Session["User_ID"].ToString()), DateTime.Now); 
         if (dsDownload.Tables["Download"].Rows.Count > 0) 
         { 
          DataRow drDownload = dsDownload.Tables["Download"].Rows[0]; 

          int SumOfFileSize4Today = int.Parse(drDownload["FileSizSum"].ToString()); 

          if (SumOfFileSize4Today + int.Parse(FileSize) <= 1073741824)//1 GB = 1024*1024*1024 bytes = 1073741824 bytes 
          //if (SumOfFileSize4Today + int.Parse(FileSize) <= 100000) 
          { 
           //DataLayer.Download.InsertRow(
           //   int.Parse(Session["User_ID"].ToString()), 
           //   DateTime.Now, 
           //   FilePath.Replace(@"\\", @"\"), 
           //   FileName, 
           //   FileSize, 
           //   DateTime.Parse(FileCreationDate) 
           //  ); 
           Response.Redirect("~/HandlerForMyFE.ashx?Downloadpath=" + HttpUtility.UrlEncode(DownloadPath)); 
          } 
          else 
          { 
           ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "YouCanNotDownloadAnyMore_SizeOverload", "YouCanNotDownloadAnyMore_SizeOverload();", true); 
          } 
         } 
         else 
         { 
          if (int.Parse(FileSize) <= 1073741824) 
          //if (int.Parse(FileSize) <= 100000) 
          { 
           //DataLayer.Download.InsertRow(
           //   int.Parse(Session["User_ID"].ToString()), 
           //   DateTime.Now, 
           //   FilePath.Replace(@"\\", @"\"), 
           //   FileName, 
           //   FileSize, 
           //   DateTime.Parse(FileCreationDate) 
           //  ); 
           Response.Redirect("~/HandlerForMyFE.ashx?Downloadpath=" + HttpUtility.UrlEncode(DownloadPath)); 
          } 
          else 
          { 
           ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "YouCanNotDownloadAnyMore_SizeOverload", "YouCanNotDownloadAnyMore_SizeOverload();", true); 
          } 
         } 
        } 
        else 
        { 
         ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "YouCanNotDownloadAnyMore_CountOverload", "YouCanNotDownloadAnyMore_CountOverload();", true); 
        } 
       } 
       else 
       { 
        ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "plzLoginFirst_ForDownload", "plzLoginFirst_ForDownload();", true); 
       } 
      } 

      DirectoryInfo dir; 

      string lastpath = ""; 
      try 
      { 
       lastpath = Request["path"].ToString(); 
      } 
      catch { } 

      lblTitleInHeader.Text = "Files"; 
      lblTitleInHeader.Text += lastpath; 

      //set back 
      string[] splited = lblTitleInHeader.Text.Split('/'); 
      lblTitleInHeader.Text = ""; 
      ArrayList arName = new ArrayList(); 

      for (int i = 0; i < splited.Length; i++) 
      { 
       if (splited[i] != "") 
       { 
        arName.Add(splited[i]); 
       } 
      } 

      for (int i = 0; i < arName.Count - 1; i++) 
      { 
       if (i != arName.Count - 1) 
       { 
        lblTitleInHeader.Text += "<a href='Download.aspx?path=%2f";//%2f =/
        for (int j = 1; j < i + 1; j++) 
        { 
         lblTitleInHeader.Text += HttpUtility.UrlEncode(arName[j].ToString() + "/"); 
        } 
        lblTitleInHeader.Text += "'>" + arName[i] + "</a>" + " " + "<img class='icoSubFolder' src='../Images/Download/icoSubFolder.png' />"; 
       } 

      } 

      lblTitleInHeader.Text += arName[arName.Count - 1].ToString(); 
      lblTitleInHeader.Text = lblTitleInHeader.Text.Replace("/'>", "'>"); 

      if (lastpath != "") 
      { 
       //dir = new DirectoryInfo(Server.MapPath("~/Files/" + lastpath)); 
       dir = new DirectoryInfo(@"C:\\Files\\" + lastpath.Replace(@"/", @"\\")); 
      } 
      else 
      { 
       //dir = new DirectoryInfo(Server.MapPath("~/Files/")); 
       dir = new DirectoryInfo(@"C:\\Files\\"); 
      } 

      int count4Folders = 0; 
      foreach (DirectoryInfo d in dir.GetDirectories()) 
      { 
       count4Folders++; 

       DirectoryInfo dirSub = new DirectoryInfo(d.FullName); 
       int iDir = 0; 
       int iFile = 0; 
       foreach (DirectoryInfo subd in dirSub.GetDirectories()) 
       { 
        iDir++; 
       } 
       foreach (FileInfo f in dirSub.GetFiles("*.*")) 
       { 
        iFile++; 
       } 

       Folders += "<div class='divFoldersBody_Row'>"; 
       Folders += "<div class='divFoldersBody_Left'>"; 
       Folders += "&nbsp;"; 
       Folders += "</div>"; 
       Folders += "<div class='divFoldersBody_Name'>"; 
       Folders += "<span class='imgFolderContainer'><img class='imgFolder' src='../Images/Download/folder.png' /></span>"; 
       Folders += "<span class='FolderName'><a class='FolderLink' href='Download.aspx?path=" + HttpUtility.UrlEncode(lastpath + "/" + d.Name) + "'>"; 
       Folders += d.Name; 
       Folders += "</a></span>"; 
       Folders += "</div>"; 
       Folders += "<div class='divFoldersBody_Count'>"; 
       Folders += iDir.ToString() + " Folders & " + iFile.ToString() + " Files"; 
       Folders += "</div>"; 
       Folders += "<div class='divFoldersBody_Right'>"; 
       Folders += "&nbsp;"; 
       Folders += "</div>"; 
       Folders += "</div>"; 
      } 

      if (count4Folders == 0) 
      { 
       divFoldersHeader.Style.Add("display", "none"); 
      } 

      int count4Files = 0; 
      foreach (FileInfo f in dir.GetFiles("*.*")) 
      { 
       count4Files++; 
       Files += "<div class='divFilesBody_Row'>"; 
       Files += "<div class='divFilesBody_Left'>"; 
       Files += "&nbsp;"; 
       Files += "</div>"; 
       Files += "<div class='divFilesBody_Name'>"; 
       char[] Extension_ChAr = f.Extension.ToCharArray(); 
       string Extension_str = string.Empty; 
       int lenghth = Extension_ChAr.Length; 
       for (int i = 1; i < Extension_ChAr.Length; i++) 
       { 
        Extension_str += Extension_ChAr[i]; 
       } 
       if (Extension_str.ToLower() == "rar" || Extension_str.ToLower() == "zip" || Extension_str.ToLower() == "zipx" || Extension_str.ToLower() == "7z" || Extension_str.ToLower() == "cat") 
       { 
        Files += "<span class='imgFileContainer'><img class='imgFile-rar' src='../Images/Download/file-rar.png' /></span>"; 
       } 
       else if (Extension_str.ToLower() == "txt" || Extension_str.ToLower() == "doc" || Extension_str.ToLower() == "docx") 
       { 
        Files += "<span class='imgFileContainer'><img class='imgFile-txt' src='../Images/Download/file-txt.png' /></span>"; 
       } 
       else if (Extension_str.ToLower() == "pdf") 
       { 
        Files += "<span class='imgFileContainer'><img class='imgFile-pdf' src='../Images/Download/file-pdf.png' /></span>"; 
       } 
       else if (Extension_str.ToLower() == "jpg" || (Extension_str.ToLower() == "png") || (Extension_str.ToLower() == "gif") || (Extension_str.ToLower() == "bmp") || (Extension_str.ToLower() == "psd")) 
       { 
        Files += "<span class='imgFileContainer'><img class='imgFile-image' src='../Images/Download/file-image.png' /></span>"; 
       } 
       else if (Extension_str.ToLower() == "exe") 
       { 
        Files += "<span class='imgFileContainer'><img class='imgFile-exe' src='../Images/Download/file-exe.png' /></span>"; 
       } 
       else 
       { 
        Files += "<span class='imgFileContainer'><img class='imgFile-unknown' src='../Images/Download/file-unknown.png' /></span>"; 
       } 
       Files += "<span title='" + f.Name + "' class='FileName'>"; 
       Files += f.Name; 
       Files += "</span>"; 
       Files += "</div>"; 
       Files += "<div class='divFilesBody_FileType'>"; 
       Files += "<span style='color:red;'>" + Extension_str.ToUpper() + "</span>" + " File"; 
       //Files += Path.GetExtension(f.Name) + " File"; 
       Files += "</div>"; 
       Files += "<div class='divFilesBody_FileSize'>"; 
       Files += ConvertBytes.ToFileSize(long.Parse(f.Length.ToString())); 
       Files += "</div>"; 
       Files += "<div class='divFilesBody_FileCreationDate'>"; 
       Files += f.CreationTime; 
       Files += "</div>"; 
       Files += "<div class='divFilesBody_FileDownload'>"; 
       string Downloadpath = "/Files" + lastpath + "/" + f.Name; 
       string EncodedDownloadpath = HttpUtility.UrlEncode(Downloadpath); 
       Files += "<a class='FileLink' href='Download.aspx?path=" + HttpUtility.UrlEncode(lastpath) + "&Downloadpath=" + EncodedDownloadpath + "&FileName=" + HttpUtility.UrlEncode(f.Name) + "&FileSize=" + HttpUtility.UrlEncode(f.Length.ToString()) + "&FileCreationDate=" + HttpUtility.UrlEncode(f.CreationTime.ToString()) + "'>"; 
       Files += "<img class='imgDownload' src='../Images/Download/Download.png' />"; 
       Files += "</a>"; 
       Files += "</div>"; 
       Files += "<div class='divFilesBody_Right'>"; 
       Files += "&nbsp;"; 
       Files += "</div>"; 
       Files += "</div>"; 
      } 

      if (count4Files == 0) 
      { 
       divFilesHeader.Style.Add("display", "none"); 
      } 

      if ((count4Folders == 0) && (count4Files == 0)) 
      { 
       divFoldersHeader.Style.Add("display", "block"); 
       divFilesHeader.Style.Add("display", "block"); 
      } 

      ScriptManager.RegisterStartupScript(this, this.GetType(), "hash", "location.hash = '#BreadCrumbInDownload';", true); 
     } 

    .... 

我的aspx:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
    <link href="css/Download.css" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript"> 
     $(function() { 
      $('#Download-body *').css({ 'zIndex': 2 }); 
      $('#Download-body').css({ 'overflow': 'hidden', 'position': 'relative' }); 
      var containerWidth = $('#Download-body').innerWidth(); 
      var containerHeight = $('#Download-body').innerHeight(); 
      var bgimgurl = $('#Download-body').css('backgroundImage').replace(/url\(|\)|"|'/g, ""); 
      var img = $('<img src="' + bgimgurl + '" width="' + containerWidth + 'px"' + ' height="' + containerHeight + 'px" />').css({ 'position': 'absolute', 'left': 0, 'top': 0, 'zIndex': 1 }); 
      $('#Download-body').css({ 'background': 'transparent' }).append(img); 
     }); 
    </script> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    <div id="Download"> 
     <div id="Download-header"> 
      <div id="DownloadTitle"> 
       Download 
      </div> 
      <asp:Image ID="imgDownload_header_left" runat="server" ImageUrl="~/Images/Download-header-left.png" /> 
      <asp:Image ID="imgDownload_header_right" runat="server" ImageUrl="~/Images/Download-header-right.png" /> 
     </div> 
     <div id="Download-body"> 
      <div id="Download-body-inside"> 
       <div id="Statistics"> 
        <div id="Statistics-header"> 
         <div id="StatisticsTitle"> 
          Statistics 
         </div> 
         <asp:Image ID="imgStatistics_header_left" runat="server" ImageUrl="~/Images/Statistics-header-left.png" /> 
         <asp:Image ID="imgStatistics_header_right" runat="server" ImageUrl="~/Images/Statistics-header-right.png" /> 
        </div> 
        <div id="Statistics-body"> 
         <div runat="server" id="Statistics_body_inside"> 
          <asp:Label ID="lblDownload_Count_By_UserID_Title" runat="server" Text="Ur Download Count :" 
           ToolTip="Your Download Count From The Begining Of Registration UpTo Now" CssClass="lblTitleInStatistics"></asp:Label> 
          <asp:Label ID="lblDownload_Count_By_UserID" runat="server" Text="<%# Download_Count_By_UserID() %>" 
           CssClass="lblCountInStatistics"></asp:Label> 
          <br /> 
          <asp:Label ID="lblDownload_Count_By_UserID_Today_Title" runat="server" Text="Ur Download Count-Today :" 
           ToolTip="Your Download Count-Today" CssClass="lblTitleInStatistics"></asp:Label> 
          <asp:Label ID="lblDownload_Count_By_UserID_Today" runat="server" Text="<%# Download_Count_By_UserID_Today() %>" 
           CssClass="lblCountInStatistics"></asp:Label> 
          <br /> 
          <asp:Label ID="lblDownload_Size_By_UserID_Title" runat="server" Text="Ur Download Size :" 
           ToolTip="Your Download Size From The Begining Of Registration UpTo Now" CssClass="lblTitleInStatistics"></asp:Label> 
          <asp:Label ID="lblDownload_Size_By_UserID" runat="server" Text="<%# Download_Size_By_UserID() %>" 
           CssClass="lblCountInStatistics"></asp:Label> 
          <br /> 
          <asp:Label ID="lblDownload_Size_By_UserID_Today_Title" runat="server" Text="Ur Download Size-Today :" 
           ToolTip="Your Download Size-Today" CssClass="lblTitleInStatistics"></asp:Label> 
          <asp:Label ID="lblDownload_Size_By_UserID_Today" runat="server" Text="<%# Download_Size_By_UserID_Today() %>" 
           CssClass="lblCountInStatistics"></asp:Label> 
          <br /> 
          <asp:Label ID="lblDownload_Size_By_UserID_Today_Remain_Title" runat="server" Text="Ur Remain Download Size-Today :" 
           ToolTip="Your Remain Download Size-Today" CssClass="lblTitleInStatistics"></asp:Label> 
          <asp:Label ID="lblDownload_Size_By_UserID_Today_Remain" runat="server" Text="<%# Download_Size_By_UserID_Today_Remain() %>" 
           CssClass="lblCountInStatistics"></asp:Label> 
         </div> 
        </div> 
       </div> 
       <div id="MainDivInDownload"> 
        <div id="BreadCrumbInDownload"> 
         <div id="imgicoHomeContainer"> 
          <asp:Image ID="imgicoHome" runat="server" ImageUrl="~/Images/Download/icoHome.png" /> 
         </div> 
         <div id="lblTitleInHeaderContainer"> 
          <asp:Label ID="lblTitleInHeader" runat="server" Text=""></asp:Label> 
         </div> 
         <div style="clear: both;"> 
         </div> 
        </div> 
        <div runat="server" id="divFolders"> 
         <div runat="server" id="divFoldersHeader"> 
          <div id="divFoldersHeader_Left"> 
           <asp:Image ID="divFoldersHeader_imgLeft" runat="server" ImageUrl="~/Images/Download/divsHeader_Left.png" /> 
          </div> 
          <div id="divFoldersHeader_Name"> 
           Folders In This Folder 
          </div> 
          <div id="divFoldersHeader_Count"> 
           Total Files In This Folder 
          </div> 
          <div id="divFoldersHeader_Right"> 
           <asp:Image ID="divFoldersHeader_imgRight" runat="server" ImageUrl="~/Images/Download/divsHeader_Right.png" /> 
          </div> 
         </div> 
         <div id="divFoldersBody"> 
          <%--    <div class="divFoldersBody_Row"> 
        <div class="divFoldersBody_Left"> 
         &nbsp; 
        </div> 
        <div class="divFoldersBody_Name"> 
         <span class="imgFolderContainer"> 
          <asp:Image CssClass="imgFolder" runat="server" ImageUrl="~/Images/Download/folder.png" /> 
         </span><span class="FolderName"><a class="FolderLink" href="#">Folders In This Folder</a></span> 
        </div> 
        <div class="divFoldersBody_Count"> 
         Total Files In This Folder 
        </div> 
        <div class="divFoldersBody_Right"> 
         &nbsp; 
        </div> 
       </div>--%> 
          <%= Folders %> 
         </div> 
        </div> 
        <div runat="server" id="divFiles"> 
         <div runat="server" id="divFilesHeader"> 
          <div id="divFilesHeader_Left"> 
           <asp:Image ID="divFilesHeader_imgLeft" runat="server" ImageUrl="~/Images/Download/divsHeader_Left.png" /> 
          </div> 
          <div id="divFilesHeader_Name"> 
           Files In This Folder 
          </div> 
          <div id="divFilesHeader_FileType"> 
           File Type 
          </div> 
          <div id="divFilesHeader_FileSize"> 
           File Size 
          </div> 
          <div id="divFilesHeader_FileCreationDate"> 
           File Creation Date 
          </div> 
          <div id="divFilesHeader_FileDownload"> 
          </div> 
          <div id="divFilesHeader_Right"> 
           <asp:Image ID="divFilesHeader_imgRight" runat="server" ImageUrl="~/Images/Download/divsHeader_Right.png" /> 
          </div> 
         </div> 
         <div id="divFilesBody"> 
          <%--    <div class="divFilesBody_Row"> 
        <div class="divFilesBody_Left"> 
         &nbsp; 
        </div> 
        <div class="divFilesBody_Name"> 
         <span class="imgFileContainer"> 
          <asp:Image runat="server" ImageUrl="~/Images/Download/file.png" CssClass="imgFile" /> 
         </span><span class="FileName">Files In This Folder</span> 
        </div> 
        <div class="divFilesBody_FileType"> 
         File Type 
        </div> 
        <div class="divFilesBody_FileSize"> 
         File Size 
        </div> 
        <div class="divFilesBody_FileCreationDate"> 
         File Creation Date 
        </div> 
        <div class="divFilesBody_FileDownload"> 
         <a class="FileLink" href="#"> 
          <asp:Image CssClass="imgDownload" runat="server" ImageUrl="~/Images/Download.png" /> 
         </a> 
        </div> 
        <div class="divFilesBody_Right"> 
         &nbsp; 
        </div> 
       </div>--%> 
          <%= Files %> 
         </div> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
</asp:Content> 

在此先感謝

+0

你有任何圖像標籤,其中'src =「」'或帶有'src'屬性的圖像標籤? –

+0

看看這個SO Q和A http://stackoverflow.com/questions/2009092/page-loads-twice-in-google-chrome 它可能只是有答案。 –

+0

是的,我爲我的工作使用了一個運行時表單,並且在代碼後面有一些HTML imgs,並帶有src屬性! – MoonLight

回答

4

請在HTML看,因爲它是在渲染頁面:

每當它是目前

<img src=""/> 

雙回送可能發生,一些瀏覽器...

如果是這樣的麻煩,你可以解決它設置默認的空白,圖像每個按鈕

<asp:ImageButton ImageUrl="~/Images/blank.gif"... 
+0

+1:正是我想要建議的。看到這麼多人被燒了。 – leppie

+0

感謝您的回答,但互聯網下載管理器和這些src屬性之間有什麼關係?與定期下載(點擊錨)我沒有兩次running.besides我檢查了我的代碼後面。沒有img與空src!任何想法? – MoonLight

+0

我相信,針對缺少資源的屬性可能會發生相同的行爲。但我認爲這是一回事! –