最近在做项目遇到一个问题,在主页面点击某个按钮时打开一个页面,之前做法都是弹出一个页面,但是现在要求是打开一个tab页面而不是弹出方式打开,这个主页面会传querystring过去,根据T_equ参数去做判断要不要默认进去让页面处于新增状态;这个不难,关键问题是需求要求这个打开的tab页面当点刷新按钮时不做处于新增状态的处理,之前是根据Request.QueryString["T_equ"]来做判断的,只要有值就让页面处于新增状态,需求是:当刷新页面或者按f5键时不让页面处于新增状态。这样一来就难解决了。Request.QueryString是只读的不能删除。所以不能把这个给清空掉,后来又考虑到用session或者viewstate去做记录,以为是多个页面都是类似的功能,为了防止可能会定义重复的session或者viewstate所以这个方案也排除掉了。一下为两种解决方案:
一种是根据参数做重定向处理,重定项时把T_equ参数清空掉处理,另一种是主页面传过来菜单码和guid在弹出页面做记录,如果文件中存在这个菜单码和guid时就当做是刷新页面处理不让页面处于新增状态,如果没找到的话就让页面处于新增状态并向文件中写入记录mc和guid.
(1)第一种方案:
bool isKeyFound = false; // Vital to stop calling recursively
string newQueryString = GetNewQueryString(Request.QueryString, "Refresh", out isKeyFound);
if (isKeyFound && newQueryString.Length > 0)
{
Response.Redirect(Request.Path + "?" + newQueryString);
}
一种是根据参数做重定向处理,重定项时把T_equ参数清空掉处理,另一种是主页面传过来菜单码和guid在弹出页面做记录,如果文件中存在这个菜单码和guid时就当做是刷新页面处理不让页面处于新增状态,如果没找到的话就让页面处于新增状态并向文件中写入记录mc和guid.
(1)第一种方案:
bool isKeyFound = false; // Vital to stop calling recursively
string newQueryString = GetNewQueryString(Request.QueryString, "Refresh", out isKeyFound);
if (isKeyFound && newQueryString.Length > 0)
{
Response.Redirect(Request.Path + "?" + newQueryString);
}
public static string GetNewQueryString(System.Collections.Specialized.NameValueCollection queryString, string removeKey, out bool isKeyFound)
{
string returnVal = string.Empty;
isKeyFound = false;
//Build the new query string.
for (int j = 0; j < queryString.Count; j++)
{
// Anything except the target key
if (j!=3&&!queryString.GetKey(j).ToLower().Equals(removeKey.ToLower()))
{
returnVal += queryString.GetKey(j) + "=" + queryString.Get(j) + "&";
}
else
{
if (j != 3)
{
isKeyFound = true;
}
}
}
//Remove the trailing '&'
return returnVal.TrimEnd('&');
}
(2)第二中方案:
tequ = Request.QueryString["T_equ"] != null ? Request.QueryString["T_equ"].ToString() : string.Empty;
mc = Request.QueryString["mc"] != null ? Request.QueryString["mc"].ToString() : string.Empty;
guid = Request.QueryString["Guid"] != null ? Request.QueryString["Guid"].ToString() : string.Empty;
bool isKeyFound = false;
if (!String.IsNullOrEmpty(mc) && !String.IsNullOrEmpty(guid))
{
ReadFile(mc, guid,out isKeyFound);
if (!String.IsNullOrEmpty(tequ) && !isKeyFound)
{
AddLoadExcuteScript("SetDefaultAddStatus();");
WgSemUse.StrFunAfterClientInitSelect = "SetDefaultAddStatus";
WriteFile(mc + "," +guid+",");
}
}
///
/// 根据mc和guid读取文件
///
///
///
///
public void ReadFile(string mc, string guid, out bool isKeyFound)
{
string curr = HttpContext.Current.Request.PhysicalApplicationPath + "Document\\SemUseCertificateInfoUrl.txt";
isKeyFound = false;
if (File.Exists(curr))
{
FileStream fs = new FileStream(curr, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader tr = new StreamReader(curr, System.Text.Encoding.Default);
tr.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = tr.ReadToEnd().Trim();
string[] temp = strLine.Split(',');
if (temp.Length > 0)
{
for (int i = 0; i < temp.Length; i++)
{
if (temp[i] == mc)
{
continue;
}
if (temp[i] == guid)
{
isKeyFound = true;
break;
}
}
}
tr.Close();
tr.Dispose();
}
{
string returnVal = string.Empty;
isKeyFound = false;
//Build the new query string.
for (int j = 0; j < queryString.Count; j++)
{
// Anything except the target key
if (j!=3&&!queryString.GetKey(j).ToLower().Equals(removeKey.ToLower()))
{
returnVal += queryString.GetKey(j) + "=" + queryString.Get(j) + "&";
}
else
{
if (j != 3)
{
isKeyFound = true;
}
}
}
//Remove the trailing '&'
return returnVal.TrimEnd('&');
}
(2)第二中方案:
tequ = Request.QueryString["T_equ"] != null ? Request.QueryString["T_equ"].ToString() : string.Empty;
mc = Request.QueryString["mc"] != null ? Request.QueryString["mc"].ToString() : string.Empty;
guid = Request.QueryString["Guid"] != null ? Request.QueryString["Guid"].ToString() : string.Empty;
bool isKeyFound = false;
if (!String.IsNullOrEmpty(mc) && !String.IsNullOrEmpty(guid))
{
ReadFile(mc, guid,out isKeyFound);
if (!String.IsNullOrEmpty(tequ) && !isKeyFound)
{
AddLoadExcuteScript("SetDefaultAddStatus();");
WgSemUse.StrFunAfterClientInitSelect = "SetDefaultAddStatus";
WriteFile(mc + "," +guid+",");
}
}
///
/// 根据mc和guid读取文件
///
///
///
///
public void ReadFile(string mc, string guid, out bool isKeyFound)
{
string curr = HttpContext.Current.Request.PhysicalApplicationPath + "Document\\SemUseCertificateInfoUrl.txt";
isKeyFound = false;
if (File.Exists(curr))
{
FileStream fs = new FileStream(curr, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader tr = new StreamReader(curr, System.Text.Encoding.Default);
tr.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = tr.ReadToEnd().Trim();
string[] temp = strLine.Split(',');
if (temp.Length > 0)
{
for (int i = 0; i < temp.Length; i++)
{
if (temp[i] == mc)
{
continue;
}
if (temp[i] == guid)
{
isKeyFound = true;
break;
}
}
}
tr.Close();
tr.Dispose();
}
}
///
/// 写入文件
///
///
public void WriteFile(string data)
{
string curr = HttpContext.Current.Request.PhysicalApplicationPath + "Document\\SemUseCertificateInfoUrl.txt";
bool isexist= File.Exists(curr);
if (isexist)
{
//StreamWriter streamw = File.AppendText(@"" + HttpContext.Current.Request.PhysicalApplicationPath + "/SemUseCertificateInfoUrl.txt");
StreamWriter streamw = File.AppendText(curr);
streamw.WriteLine(data);
streamw.Close();
streamw.Dispose();
}
else
{
File.Create(curr).Close();
}
///
/// 写入文件
///
///
public void WriteFile(string data)
{
string curr = HttpContext.Current.Request.PhysicalApplicationPath + "Document\\SemUseCertificateInfoUrl.txt";
bool isexist= File.Exists(curr);
if (isexist)
{
//StreamWriter streamw = File.AppendText(@"" + HttpContext.Current.Request.PhysicalApplicationPath + "/SemUseCertificateInfoUrl.txt");
StreamWriter streamw = File.AppendText(curr);
streamw.WriteLine(data);
streamw.Close();
streamw.Dispose();
}
else
{
File.Create(curr).Close();
}
}