当前位置:  开发笔记 > 编程语言 > 正文

如何防止两次Page_Load运行 - Page.IsPostBack始终为false

如何解决《如何防止两次Page_Load运行-Page.IsPostBack始终为false》经验,为你挑选了1个好方法。

in a strange situation my page_Load Runs twice and all of my codes are in page_Load.
i can not use Page.IsPostBac because in both of Page_Loads, it is false.
but what is strange situation ?
i have a page for download files in my project,
when you click on one of download links (i used anchors, so IsPostBack is false) page_Load runs and in page load i check DownloadPath Querystring.
if DownloadPath not be null i jump to a Handler.ashx by passing that DownloadPath for showing Download Window To My Users.
during this process i have some logs in my database and because of that twice running they will be duplicate.
but when twice running accours ?
when you try to download a link by internet download manager, page_Load runs twice!

我如何防止第二次运行page_Load或有没有办法识别它?

编辑:
我的页面_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 += "" + arName[i] + "" + " " + "";
                }

            }

            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 += "
"; Folders += "
"; Folders += " "; Folders += "
"; Folders += "
"; Folders += ""; Folders += ""; Folders += d.Name; Folders += ""; Folders += "
"; Folders += "
"; Folders += iDir.ToString() + " Folders & " + iFile.ToString() + " Files"; Folders += "
"; Folders += "
"; Folders += " "; Folders += "
"; Folders += "
"; } if (count4Folders == 0) { divFoldersHeader.Style.Add("display", "none"); } int count4Files = 0; foreach (FileInfo f in dir.GetFiles("*.*")) { count4Files++; Files += "
"; Files += "
"; Files += " "; Files += "
"; Files += "
"; 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 += ""; } else if (Extension_str.ToLower() == "txt" || Extension_str.ToLower() == "doc" || Extension_str.ToLower() == "docx") { Files += ""; } else if (Extension_str.ToLower() == "pdf") { Files += ""; } else if (Extension_str.ToLower() == "jpg" || (Extension_str.ToLower() == "png") || (Extension_str.ToLower() == "gif") || (Extension_str.ToLower() == "bmp") || (Extension_str.ToLower() == "psd")) { Files += ""; } else if (Extension_str.ToLower() == "exe") { Files += ""; } else { Files += ""; } Files += ""; Files += f.Name; Files += ""; Files += "
"; Files += "
"; Files += "" + Extension_str.ToUpper() + "" + " File"; //Files += Path.GetExtension(f.Name) + " File"; Files += "
"; Files += "
"; Files += ConvertBytes.ToFileSize(long.Parse(f.Length.ToString())); Files += "
"; Files += "
"; Files += f.CreationTime; Files += "
"; Files += "
"; string Downloadpath = "/Files" + lastpath + "/" + f.Name; string EncodedDownloadpath = HttpUtility.UrlEncode(Downloadpath); Files += ""; Files += ""; Files += ""; Files += "
"; Files += "
"; Files += " "; Files += "
"; Files += "
"; } 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:


    
    


    
Download
Statistics




Folders In This Folder
Total Files In This Folder
<%--
 
Total Files In This Folder
 
--%> <%= Folders %>
Files In This Folder
File Type
File Size
File Creation Date
<%--
 
Files In This Folder
File Type
File Size
File Creation Date
 
--%> <%= Files %>

提前致谢



1> Emanuele Gre..:

请查看页面上呈现的html:

每次它出现


对于某些浏览器,可能会发生双重回发...

如果这是麻烦,您可以解决它为每个按钮设置默认,空白,图像

推荐阅读
凹凸曼00威威_694
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有