Asp.net 他一定是问题的根源

Asp.net 他一定是问题的根源,asp.net,sql-server,session,session-state,session-timeout,Asp.net,Sql Server,Session,Session State,Session Timeout,无论如何,在会话中存储控件不是一个好主意。只需存储能够重新生成控件的几个可序列化属性(PageSize和CurrentPageIndex),或者以查询字符串的形式传递它们 如果可以,也可以使用ViewState 关于在会话中存储DataTable,如果您有大量数据和许多连接的用户,这可能是一个非常糟糕的主意。这是因为我在分页中使用了大量会话来存储吗?我应该使用另一个代码进行分页吗?或者我应该修改任何使所有会话对象可序列化的方法吗?停止使用CAPSLOCK我们不会盲目尝试下次在发布重复问题之前使用

无论如何,在会话中存储控件不是一个好主意。只需存储能够重新生成控件的几个可序列化属性(PageSize和CurrentPageIndex),或者以查询字符串的形式传递它们

如果可以,也可以使用ViewState


关于在会话中存储DataTable,如果您有大量数据和许多连接的用户,这可能是一个非常糟糕的主意。

这是因为我在分页中使用了大量会话来存储吗?我应该使用另一个代码进行分页吗?或者我应该修改任何使所有会话对象可序列化的方法吗?停止使用CAPSLOCK我们不会盲目尝试下次在发布重复问题之前使用搜索。请检查:谢谢你的评论,但我已经看到了这一点。这是我自己选择的天气是否使用大写锁定,但谢谢你的建议。如果您停止提供建议并开始提供解决方案,那将是非常好的,因为这就是为什么您在这里帮助该领域的其他新手。仅供参考,很难阅读CAPSLOCK中的任何文本来了解解决方案,也许可以代替在会话中逐页传递datatable,尝试在其他页面上传递简单ID并通过此ID直接检索所需的数据这是因为我在页面中使用了大量会话来存储的问题吗?我应该使用另一个代码进行分页吗?或者我应该修改任何使所有会话对象可序列化的方法吗?停止使用CAPSLOCK我们不会盲目尝试下次在发布重复问题之前使用搜索。请检查:谢谢你的评论,但我已经看到了这一点。这是我自己选择的天气是否使用大写锁定,但谢谢你的建议。如果您停止提供建议并开始提供解决方案,那将是非常好的,因为这就是为什么您在这里帮助该领域的其他新手。仅供参考,很难阅读CAPSLOCK中的任何文本来了解解决方案,也许可以代替在会话中逐页传递datatable,尝试在其他页面上传递简单ID并通过该ID直接检索所需的数据在会话中存储int值是基本的会话处理。关于viewstate,请看一下在会话中存储int值是基本的会话处理。关于viewstate,请查看
 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string query = "select xxxxqueryxxxx";
        SqlDataAdapter da = new SqlDataAdapter(query, con);
        DataSet ds = new DataSet();
        try
        {
            using (con)
            {
                con.Open();
                da.Fill(ds);
            }
        }
        catch
        {
            ds = null;
        }
        finally
        {
            if (ds != null)
            {
                CustomPaging page = new CustomPaging();
                DataTable dt = ds.Tables[0];
                page.PageSize = 10;
                page.DataSource = dt;
                page.CurrentPageIndex = 0;
                no = 1;
                Session["DT"] = dt;
                Session["page"] = page;
                bindData(page, dt);

                //set these properties for multi columns in datalist
                DataList2.RepeatColumns = 1;
                DataList2.RepeatDirection = RepeatDirection.Horizontal;
            }
        }
    }
}
void bindData(CustomPaging page, DataTable dt)
{
    try
    {
        DataList2.DataSource = page.DoPaging;
        DataList2.DataBind();
        //DataList2.DataSource = SqlDataSource1;
        //DataList2.DataBind();
        lbtnPre.Enabled = !page.IsFirstPage; //Enable / Disable Navigation Button
       // lbtnPre.CssClass = "disabledbtn";
        lbtnNext.Enabled = !page.IsLastPage;
        //lbtnNext.CssClass = "disabledbtn";
        lblStatus.Text = NavigationIndicator(); //Build Navigation Indicator 
        //for creating page index
        DataTable dt1 = new DataTable();
        dt1.Columns.Add("PageIndex");
        dt1.Columns.Add("PageText");

        for (int i = 0; i < page.PageCount; i++)
        {
            DataRow dr = dt1.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt1.Rows.Add(dr);
        }
        dlPaging.DataSource = dt1;
        dlPaging.DataBind();
        dlPaging.RepeatColumns = 10;
        dlPaging.RepeatDirection = RepeatDirection.Horizontal;
    }
    catch (Exception)
    {
    }
    finally
    {
        page = null;
    }
}
string NavigationIndicator()
{
    string str = string.Empty;     //Page x Of Y
    str = Convert.ToString(((CustomPaging)Session["page"]).CurrentPageIndex + 1) + " of " + ((CustomPaging)Session["PAGE"]).PageCount.ToString() + " Page(s) found";
    return str;
}
protected void lbtnPre_Click(object sender, EventArgs e)
{
    int pageIndex = ((CustomPaging)Session["page"]).CurrentPageIndex;
    if (!((CustomPaging)Session["page"]).IsFirstPage)
        //Decrements the pageIndex by 1 (Move to Previous page)
        ((CustomPaging)Session["page"]).CurrentPageIndex -= 1;
    else
        ((CustomPaging)Session["page"]).CurrentPageIndex = pageIndex;

    //Binds the DataList with new pageIndex
    bindData(((CustomPaging)Session["page"]), ((DataTable)Session["DT"]));
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
    int pageIndex = ((CustomPaging)Session["page"]).CurrentPageIndex;
    if (!((CustomPaging)Session["page"]).IsLastPage)
        //Increments the pageIndex by 1 (Move to Next page)
        ((CustomPaging)Session["page"]).CurrentPageIndex += 1;
    else
        ((CustomPaging)Session["page"]).CurrentPageIndex = pageIndex;

    //Binds the DataList with new pageIndex
    bindData(((CustomPaging)Session["page"]), ((DataTable)Session["DT"]));
}
protected void DataList2_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Redirect("Default2.aspx?partnumber=" + DataList2.DataKeyField[DataList2.SelectedIndex].ToString());
}
protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        no = int.Parse(e.CommandArgument.ToString()) + 1;
        ((CustomPaging)Session["page"]).CurrentPageIndex = int.Parse(e.CommandArgument.ToString());
        //Binds the DataList with new pageIndex
        bindData(((CustomPaging)Session["page"]), ((DataTable)Session["DT"]));

    }
}
protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
    LinkButton btn = (LinkButton)e.Item.FindControl("lnkbtnPaging");

    if (btn.Text == no.ToString())
    {
        btn.ForeColor = System.Drawing.Color.Maroon;
        btn.Font.Underline = false;
    }
    else
    {
        btn.ForeColor = System.Drawing.Color.DarkCyan;
        btn.Font.Underline = false;
    }
}