基础太差,代码搞不定了,得不到值,特来求助
自己写了个自定义分页控件如下
public int recordcount;
public int size;
public int curPage;
public string condition;
protected void Page_Load(object sender, EventArgs e)
{
int allpage;
if (recordcount % size == 0)
allpage = recordcount / size;
else
allpage = recordcount / size + 1;
if (allpage == 0)
allpage = 1;
//获取链接地址
if (curPage != 1)
{
this.hplback.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage - 1)+condition;
this.hplfirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1"+condition;
}
if (curPage != allpage)
{
this.hplnext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage + 1)+condition;
this.hplend.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + allpage+condition;
}
}
在aspx页里使用ID为aspnetpage
不查询分页,还行,一查询分页就报:
行 19: {
行 20: int allpage;
行 21: if (recordcount % size == 0)
aspx自带的CS代码如下:
private int size = 2;
private int curPage;
static string sql;
protected void Button1_Click(object sender, EventArgs e)
{
sql = "select * from food where name like '" + TextBox1.Text.ToString() + "'";
DataTable dt = ddw.dt(sql);
if (Request.QueryString["Page"] != null)
curPage = Convert.ToInt32(Request.QueryString["Page"]);
else
curPage = 1;
AspNetPage.recordcount = dt.Rows.Count;
AspNetPage.size = 2;
AspNetPage.curPage = curPage;
}
------解决方案--------------------
public int recordcount;
public int size;
这些变量的值丢了吧
------解决方案--------------------
页面回发的时候变量值没保存起来
这样声明:
C# code
public int recordcount
{
get
{
return ViewState["recordcount"]==null?0:(int)ViewSate["recordcount"];
}
set
{
ViewState["recordcount"] = value;
}
}
------解决方案--------------------
调用控件的页面在page_load里加if(!page.ispostback){。。。}没有?
------解决方案--------------------
public int recordcount;
public int size;
public int curPage;
public string condition;
问题在这里,页面一刷新,值全清了
------解决方案--------------------
lz那样写,怎么都不好弄
可以把那些需要接收调用时赋值的变量改成session
加载页面的时候首先给session赋值