日期:2014-05-17  浏览次数:20481 次

请教,带翻页页面的全局变量问题
C# code

public partial class queryNoAppCardDept : System.Web.UI.Page
{
    public string HangSh = "0";//行数
    public string YeSh = "0";//页数
    public static string s = "";
    DataView dv = new DataView();
    public Int32 CurrentPageIndex;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
        }
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
            string strSql = "";
            
        AspNetPager1.CurrentPageIndex = 1;
                
        if (ddlApartment.SelectedValue.Length == 2)
        {
            s = "where .................1";
        }
        else
        {
            s = "where .................2";
        }

        int curPage = 0;
        
        if (Request.QueryString["page"] != null)
        {
            curPage = Convert.ToInt32(Request.QueryString["page"]);
        }
        else
        {
            curPage = 1;
        }

        CurrentPageIndex = curPage;

        strSql = "select * from table1 " + s + " order by personno";
        dv = dataOperate.getDataView(strSql, curPage, "20", out YeSh, out HangSh);
        AspNetPager1.RecordCount = Convert.ToInt32(HangSh);
        bindData();
    }

    void bindData()
    {
        Repeater1.DataSource = dv;
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
            string strSql = "";
            int curPage = 0;
            
        AspNetPager1.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
        
        if (Request.QueryString["page"] != null)
        {
            curPage = Convert.ToInt32(Request.QueryString["page"]);
        }
        else
        {
            curPage = 1;
        }

        CurrentPageIndex = curPage;

        if (s == "")
        {
            //xxxxxxxx
        }
        else
        {
            strSql = "select from table1 " + s + " order by personno";
            dv = dataOperate.getDataView(strSql, curPage, "20", out YeSh, out HangSh);
            AspNetPager1.RecordCount = Convert.ToInt32(HangSh);
                AspNetPager1.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
                bindData();
        }        
    }
}



主要就是这个public static string s = "";
此处用static显然不合适,因为肯定不会只有一个人来访问该页面。

试过session,这个倒是可以,但是短时间内回来在访问这个页面的时候,因为session还有效,这个页面还会用session的条件继续自动查询并显示结果,而我希望离开页面再回来session是空的。

也试过ViewStatus,但是翻页就有问题,不知道怎么解决这个问题?请指教!

------解决方案--------------------
这个事情啊,你没看到你代码里面有重复的应用么?

我给你改下!

C# code

public partial class queryNoAppCardDept : System.Web.UI.Page
{
    public string HangSh = "0";//行数
    public string YeSh = "0";//页数
    public static string s = "";
    DataView dv = new DataView();
    public Int32 CurrentPageIndex;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
        }
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
 
        bindData();
    }

    void bindData()
    {
                    string strSql = "";
            
        AspNetPager1.CurrentPageIndex = 1;
                
        if (ddlApartment.SelectedValue.Length == 2)
        {
            s = "where .................1";
        }
        else
        {
            s = "where .................2";
        }

        int curPage = 0;
        
        if (Request.QueryString["page"] != null)