C# 如何防止两次页面加载运行-Page.IsPostBack始终为false
在一种奇怪的情况下,我的页面加载运行了两次,所有代码都在页面加载中。C# 如何防止两次页面加载运行-Page.IsPostBack始终为false,c#,asp.net,pageload,C#,Asp.net,Pageload,在一种奇怪的情况下,我的页面加载运行了两次,所有代码都在页面加载中。 我不能使用Page.IsPostBac,因为在这两个页面加载中,它都是false。 但什么是奇怪的情况? 我的项目中有一个下载文件的页面, 当您单击其中一个下载链接(我使用了锚,所以IsPostBack为false)时,页面加载将运行,并在页面加载中检查下载路径查询字符串。 如果DownloadPath不为null,我会将下载路径传递给我的用户以显示下载窗口,从而跳转到Handler.ashx。 在此过程中,我的数据库中有一些
我不能使用
Page.IsPostBac
,因为在这两个页面加载中,它都是false。但什么是奇怪的情况?
我的项目中有一个下载文件的页面,
当您单击其中一个下载链接(我使用了锚,所以IsPostBack为false)时,页面加载将运行,并在页面加载中检查下载路径查询字符串。
如果DownloadPath不为null,我会将下载路径传递给我的用户以显示下载窗口,从而跳转到Handler.ashx。
在此过程中,我的数据库中有一些日志,因此会重复运行两次。
但当两次运行accours时?
当您尝试通过internet下载管理器下载链接时,页面加载将运行两次 如何防止第二次运行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 += " ";
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 += " ";
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 += " ";
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 += " ";
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);
}
....
fileexplorer.en
{
公共部分类下载:System.Web.UI.Page
{
公共字符串文件夹=”;
公共字符串文件=”;
受保护的无效页面加载(对象发送方、事件参数e)
{
Statistics_body_inside.DataBind();
if(Request.QueryString[“Path”!=null)
{
if(Request.QueryString[“Path”]=“.”)
{
ScriptManager.RegisterStartupScript(this.Page,typeof(Page),“youcannotdothatore”,“youcannotdothatore();”,true);
}
其他的
{
MainCodes();
}
}
其他的
{
MainCodes();
}
}
专用代码()
{
if(Request.QueryString[“DownloadPath”!=null)
{
string DownloadPath=Request.QueryString[“DownloadPath”].ToString();
string FilePath=“C:“+DownloadPath.Replace(@”/“,@“\\”);
如果(会话[“用户ID”!=null)
{
string FileName=Request.QueryString[“FileName”].ToString();
string FileSize=Request.QueryString[“FileSize”].ToString();
字符串FileCreationDate=Request.QueryString[“FileCreationDate”].ToString();
int Download_Count_4Today=DataLayer.Download.Count_By_UserID_Today(int.Parse(HttpContext.Current.Session[“User\u ID”].ToString()),DateTime.Now);
如果(今天下载的\u计数\u 4 0)
{
DataRow drDownload=dsDownload.Tables[“Download”]。行[0];
int-SumOfFileSize4Today=int.Parse(drDownload[“filesizzum”].ToString());
if(SumOfFileSize4Today+int.Parse(文件大小)
--%>
提前感谢请查看页面上呈现的html: 每次它出现的时候
<img src=""/>
对于某些浏览器,可能会发生双重回发
如果这是问题所在,您可以通过为每个按钮设置默认的空白图像来解决
<asp:ImageButton ImageUrl="~/Images/blank.gif"...
请查看页面上呈现的html:
每次它出现的时候
<img src=""/>
对于某些浏览器,可能会发生双重回发
如果这是问题所在,您可以通过为每个按钮设置默认的空白图像来解决
<asp:ImageButton ImageUrl="~/Images/blank.gif"...
您是否有任何图像标记位于src=”“
或具有src
属性的图像标记?请查看此,这样Q和a可能就有了答案。是的,我在我的工作中使用了运行时表单,我在代码隐藏中有一些带有src属性的HTML IMG!您有任何图像标记位于src=“”
或带有src
属性的图像标签?看看这个,Q和a可能就知道答案了。是的,我的工作使用了运行时表单,我在代码中有一些带有src属性的HTML IMG!+1:正是我要建议的。看到这么多人被这个烧焦了。谢谢你的回答,但是intern和et下载管理器和这些src属性?定期下载(单击锚)我没有两次运行。此外,我检查了我的代码。没有带空src的img!有什么想法吗?我相信针对缺少资源的属性可能会出现相同的行为。但我猜大致上是一样的!哇,gusy,请查看我的编辑(aspx代码)再说一次。你觉得上面的脚本怎么样?我把它添加到书签中,仍然存在这个问题!似乎第二个页面加载是因为我的aspx。我的页面基于母版页和内容页,右键单击链接并选择下载IDM->1。页面加载下载(内容)page accours 2.page_Load Of Master page accours 3.它返回到内容页aspx和4.最后我们又有了page_Load Of Content page!+1:正是我要建议的。看到这么多人被这烧焦了。谢谢你的回答,但是internet下载管理器和这些src属性之间有什么关系?定期下载(单击锚)我没有两次运行。此外,我检查了我的代码。没有带空src的img!有什么想法吗?我相信针对缺少资源的属性可能会出现相同的行为。但我猜大致上这是一样的!哇,gusy,请查看我的编辑(aspx代码)再说一次。你觉得上面的脚本怎么样?我把它书签了,仍然有那个问题!似乎第二页是因为和在我的aspx.my中