···怎样解决通过session处理分页和搜索后出现的问题···

bertieone 2008-07-14 04:16:43


这是一段分页程序,在分页按钮中,因为我是用Redirect方法处理的,所以为了防止点击分页按钮以后回到默认页面,我就把搜索框textbox里的值写入session,这样可以在点击分页后仍然保存这个值,但现在我遇到问题是,当我搜索一个结果后,如果我通过点击ie后退按钮返回到首页,或者直接把网站地址后面的参数都删掉然后回车到首页,之后我再点刷新或者分页,他都会跳回之前的搜索结果页。
我知道这是用了session的结果。但我必须在代码改动不大的前提下解决这问题,要麻烦大家帮我看一下了



public partial class index : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
public int PageSize = 5;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["game"] != null)
{
SerchGame.Text = Session["game"].ToString();
}

if (Session["sn"] != null)
{
SerchSn.Text = Session["sn"].ToString();
}

if (Session["times"] != null)
{
SerchTimes.Text = Session["times"].ToString();
}
DefaultPage();
}

}


protected void btnserch_Click(object sender, EventArgs e) //搜索按钮代码开始
{
if (Session["sn"] != null)
{
Session.Remove("sn");
}

if (Session["game"] != null)
{
Session.Remove("game");
}

if (Session["times"] != null)
{
Session.Remove("times");
}


if (SerchGame.Text == null || SerchSn.Text != null || SerchTimes.Text == null)
{
Session["sn"] = SerchSn.Text;
}

if (SerchGame.Text != null || SerchSn.Text == null || SerchTimes.Text == null)
{
Session["game"] = SerchGame.Text;
}

if (SerchGame.Text == null || SerchSn.Text == null || SerchTimes.Text != null)
{
Session["times"] = SerchTimes.Text;
}

DefaultPage();
Response.Redirect("index.aspx?PageID=1");
} //搜索按钮代码结束



protected void DefaultPage() //分页方法开始
{
conn.Open();
string sql1;

if (SerchGame.Text == "" && SerchSn.Text == "" && SerchTimes.Text == "") //如果textbox内为空则执行默认sql,否则执行else
{
sql1 = "select count(*) from [order] ";
}
else
{
sql1 = "select count(*) from [order] where sn='" + Session["sn"] + "'or game='" + Session["game"] + "' or times='" + Session["times"] + "'";
}

SqlCommand cmd = new SqlCommand(sql1, conn);
int MaxSize = Convert.ToInt32(cmd.ExecuteScalar());
int Page = Convert.ToInt32(Request.QueryString.Get("PageID"));

if (Page == 0 || Request.Form["btnserch"]!=null)
{
Page = 1;
}

float temp = (float)MaxSize / PageSize;

if (Page >= Convert.ToInt32(Math.Ceiling(temp)))
{
Page = Convert.ToInt32(Math.Ceiling(temp));
}

string sql2;
if (SerchGame.Text == "" && SerchSn.Text == "" && SerchTimes.Text == "") //如果textbox内为空则执行默认sql,否则执行else
{
sql2 = "select count(*) from (select top " + PageSize * Page + " * from [order] ) as c";
}
else
{
sql2 = "select count(*) from (select top " + PageSize * Page + " * from [order] where sn='" + Session["sn"] + "'or game='" + Session["game"] + "' or times='" + Session["times"] + "') as c";
}

SqlCommand scmd1 = new SqlCommand(sql2, conn);
SqlDataReader sdr1 = scmd1.ExecuteReader();

sdr1.Read();
string cou = sdr1[0].ToString();
sdr1.Close();

int RowCount = Int32.Parse(cou) - PageSize * (Page - 1);

string SQL;
if (SerchGame.Text == "" && SerchSn.Text == "" && SerchTimes.Text == "") //如果textbox内为空则执行默认sql,否则执行else
{
SQL = "select * from (select top " + RowCount.ToString() + " * from (select top " + Page * PageSize + " * from [order] order by id DESC) as a order by a.id ) as b order by b.id DESC";
}
else
{
SQL = "select * from (select top " + RowCount.ToString() + " * from (select top " + Page * PageSize + " * from [order] where sn = '" + Session["sn"] + "' or game='" + Session["game"] + "' or times='" + Session["times"] + "' order by id DESC) as a order by a.id ) as b order by b.id DESC";
}

SqlDataAdapter sda = new SqlDataAdapter(SQL, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "[order]");
DataTable dt = ds.Tables["[order]"];
conn.Close();

foreach (DataRow row in dt.Rows) //输出html表格
{
Response.Write("<table width=350 height=20 border=0 cellspacing=0 cellpadding=0><tr><td width=150><a href=orderdetail.aspx?id=" + row["id"] + ">" + (row["sn"]) + "</a></td><td width=150><a href=orderdetail.aspx?game=" + row["game"] + ">" + (row["game"]) + "</a></td><td width=150><a href=orderdetail.aspx?times=" + row["times"] + ">" + (row["times"]) + "</a></td></tr></table>");
}

conn.Close();
LblTitle.Text = "共有" + Convert.ToInt32(Math.Ceiling(temp)) + "页/当前是第" + Page + "页";
} //分页方法结束




//第一页--------------------------------------------------
protected void BtnFirst_Click(object sender, EventArgs e)
{
Response.Redirect("index.aspx?PageID=1");
}
//第一页--------------------------------------------------


//上一页-----------------------------------------------------
protected void BtnPre_Click(object sender, EventArgs e)
{
int Page = Convert.ToInt32(Request.QueryString.Get("PageID"));
if (Page <= 1)
{
Page = 2;
}
int curent = Page - 1;
Response.Redirect("index.aspx?PageID=" + curent);
}
//上一页-----------------------------------------------------



//下一页-----------------------------------------------------
protected void BtnNext_Click(object sender, EventArgs e)
{
conn.Open();
string sql;

if (SerchGame.Text == "" && SerchSn.Text == "" && SerchTimes.Text == "") //如果textbox内为空则执行默认sql,否则执行else
{
sql = "select count(*) from [order] ";
}
else
{
sql = "select count(*) from [order] where sn='" + Session["sn"] + "' or game='" + Session["game"] + "' or times='" + Session["times"] + "'";
}
SqlCommand cmd = new SqlCommand(sql, conn);
int MaxSize = Convert.ToInt32(cmd.ExecuteScalar());
int Page = Convert.ToInt32(Request.QueryString.Get("PageID"));

float temp = (float)MaxSize / PageSize;

if (Page >= Convert.ToInt32(Math.Ceiling(temp)))
{
Page = Convert.ToInt32(Math.Ceiling(temp)) - 1;
}

if (Page <= 0)
{
Page = 1;
}
int curent = Page + 1;
Response.Redirect("index.aspx?PageID=" + curent);
conn.Close();
}
//下一页---------------------------

//最后一页-------------------------
protected void BtnLast_Click(object sender, EventArgs e)
{
conn.Open();
string sql;
if (SerchGame.Text == "" && SerchSn.Text == "" && SerchTimes.Text == "") //如果textbox内为空则执行默认sql,否则执行else
{
sql = "select count(*) from [order] ";
}
else
{
sql = "select count(*) from [order] where sn='" + Session["sn"] + "' or game='" + Session["game"] + "' or times='" + Session["times"] + "'";
}
SqlCommand cmd = new SqlCommand(sql, conn);
int MaxSize = Convert.ToInt32(cmd.ExecuteScalar());
float temp = (float)MaxSize / PageSize;
Response.Redirect("index.aspx?PageID=" + Math.Ceiling(temp));
conn.Close();
}
//最后一页----------------------------
}
...全文
192 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyhuc 2008-07-14
  • 打赏
  • 举报
回复
为什么分页要用session,如果停留这个页面时间长了,你可以用viewstate或者hidden 控件,或者Request.QueryString,
反正是搜索嘛,不必想得太复杂.防止别人注入就行了
ppp7p 2008-07-14
  • 打赏
  • 举报
回复
使用ViewState吧.
  • 打赏
  • 举报
回复
if(!Page.PostBack) Session["条件"]="";
allanmorgan 2008-07-14
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{

//update
if (!IsPostBack && Request.QueryString.Get("PageID") != null)
//update
{
if (Session["game"] != null)
{
SerchGame.Text = Session["game"].ToString();
}

if (Session["sn"] != null)
{
SerchSn.Text = Session["sn"].ToString();
}

if (Session["times"] != null)
{
SerchTimes.Text = Session["times"].ToString();
}
DefaultPage();
}

}


protected void btnserch_Click(object sender, EventArgs e) //搜索按钮代码开始
{
if (Session["sn"] != null)
{
Session.Remove("sn");
}

if (Session["game"] != null)
{
Session.Remove("game");
}

if (Session["times"] != null)
{
Session.Remove("times");
}


if (SerchGame.Text == null ¦ ¦ SerchSn.Text != null ¦ ¦ SerchTimes.Text == null)
{
Session["sn"] = SerchSn.Text;
}

if (SerchGame.Text != null ¦ ¦ SerchSn.Text == null ¦ ¦ SerchTimes.Text == null)
{
Session["game"] = SerchGame.Text;
}

if (SerchGame.Text == null ¦ ¦ SerchSn.Text == null ¦ ¦ SerchTimes.Text != null)
{
Session["times"] = SerchTimes.Text;
}

DefaultPage();
Response.Redirect("index.aspx?PageID=1");
} //搜索按钮代码结束

跟上边效果一样。可以一试。
verydeed 2008-07-14
  • 打赏
  • 举报
回复
呵呵,楼主需要静下心来研究一下web开发啊,这么临阵磨枪可不是长久之计啊。

详细的说一下
假设分页显示的页面为a.aspx
比如设定restore_page这个参数,是1的话就用session中的数据,没有值的话,就不用session的数据,那么url就会类似a.aspx?restore_page=1

a.aspx.cs中,在取session数据前,判断一下

以下是伪代码:
if(Request["restore_page"] == "1")
当前面 = session["页码"]
else
当前面 = 1
end

希望对你有帮助。
allanmorgan 2008-07-14
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
//update
if(Session["My_Page"] = null || Request.QueryString.Get("PageID") = null)
{ Session["My_Page"] = 0}
//update end

if (!IsPostBack && Session["My_Page"] != 0)
{
if (Session["game"] != null)
{
SerchGame.Text = Session["game"].ToString();
}

if (Session["sn"] != null)
{
SerchSn.Text = Session["sn"].ToString();
}

if (Session["times"] != null)
{
SerchTimes.Text = Session["times"].ToString();
}
DefaultPage();
}

}


protected void btnserch_Click(object sender, EventArgs e) //搜索按钮代码开始
{
if (Session["sn"] != null)
{
Session.Remove("sn");
}

if (Session["game"] != null)
{
Session.Remove("game");
}

if (Session["times"] != null)
{
Session.Remove("times");
}


if (SerchGame.Text == null || SerchSn.Text != null || SerchTimes.Text == null)
{
Session["sn"] = SerchSn.Text;
}

if (SerchGame.Text != null || SerchSn.Text == null || SerchTimes.Text == null)
{
Session["game"] = SerchGame.Text;
}

if (SerchGame.Text == null || SerchSn.Text == null || SerchTimes.Text != null)
{
Session["times"] = SerchTimes.Text;
}

//update
Session["My_Page"] = 1
//update end

DefaultPage();
Response.Redirect("index.aspx?PageID=1");
} //搜索按钮代码结束


这样试试,这么写感觉 Session["My_Page"] 几乎没什么用处,只是用了 Request.QueryString.Get("PageID") = null,不知道这么判断是否可行。以前遇到过类似问题,一时想不起来怎么处理的了。
bertieone 2008-07-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 verydeed 的回复:]
我以前也碰到过类似的问题,在这里说一下思路吧

我的办法是,在url中指明是否使用session来分布,就这么简单。这种方案下,你只需在page_load中判断一下QueryString即可。
[/Quote]
是判断url吗,那怎么定义这个字符串呢,现在有点糊涂。
李班头 2008-07-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 verydeed 的回复:]
我以前也碰到过类似的问题,在这里说一下思路吧

我的办法是,在url中指明是否使用session来分布,就这么简单。这种方案下,你只需在page_load中判断一下QueryString即可。
[/Quote]
应该这么做的,以前也碰到过!
zhoudengpan102 2008-07-14
  • 打赏
  • 举报
回复



private string sql = "语句";
private static int pageSize = 0;
System.Web.UI.WebControls.PagedDataSource pdSource = new PagedDataSource();

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.getDate(sql);
}

}
private void getDate(string sql)
{

//获得数据源
BLL.ServiceManager.BService bservice = new BLL.ServiceManager.BService();

DataSet ds = bservice.selectByString(sql);


pdSource.DataSource = ds.Tables[0].DefaultView;
pdSource.AllowPaging = true;

int currentPage = int.Parse(this.txtPage.Text.Trim());

pdSource.CurrentPageIndex = currentPage - 1;
pdSource.PageSize = 5;
pageSize = pdSource.PageCount;

this.lblCount.Text = pdSource.DataSourceCount.ToString();
this.txtSize.Text = pdSource.PageSize.ToString();
this.txtCurrentPage.Text = this.txtPage.Text;
this.lblPageSize.Text = pdSource.PageCount.ToString();

this.gvwselectassign.DataSource = pdSource;
this.gvwselectassign.DataBind();
}

protected void lkbFirst_Click(object sender, EventArgs e)
{
this.txtPage.Text = "1";
this.getDate(sql);
}
protected void lkbPrevious_Click(object sender, EventArgs e)
{
this.txtPage.Text = (int.Parse(this.txtPage.Text) - 1).ToString();
this.getDate(sql);
}
protected void lkbNext_Click(object sender, EventArgs e)
{
this.txtPage.Text = (int.Parse(this.txtPage.Text) + 1).ToString();
this.getDate(sql);
}
protected void lkbLast_Click(object sender, EventArgs e)
{
this.txtPage.Text = pageSize.ToString();
this.getDate(sql);
}

verydeed 2008-07-14
  • 打赏
  • 举报
回复
我以前也碰到过类似的问题,在这里说一下思路吧

我的办法是,在url中指明是否使用session来分布,就这么简单。这种方案下,你只需在page_load中判断一下QueryString即可。
bertieone 2008-07-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 allanmorgan 的回复:]
可以再增加一个session,专门记录页面信息(比如:首页 session(My_page) = 0,有数据查许的页面为1).在pageload的时候先判断该session的值,判断该页面是首页的话 先把保存查询条件的session清空,在查询数据的时候先改变该session值,再刷新或跳转页面。
[/Quote]



请问具体如何加在我的代码里面呢

allanmorgan 2008-07-14
  • 打赏
  • 举报
回复
可以再增加一个session,专门记录页面信息(比如:首页 session(My_page) = 0,有数据查许的页面为1).在pageload的时候先判断该session的值,判断该页面是首页的话 先把保存查询条件的session清空,在查询数据的时候先改变该session值,再刷新或跳转页面。
冬天的糊涂神 2008-07-14
  • 打赏
  • 举报
回复
把数据保存再ViewState中

62,133

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧