2013-03-13 128 views
1

我厭倦瞭解這個問題的問題。已經閱讀了這麼多的博客和論壇,但我無法找出問題所在。無法序列化會話狀態

我正在使用「SQLServer」模式來存儲會話。

一切工作正常。但每當我在我的網站使用搜索功能,它會拋出以下錯誤:

「無法序列化會話狀態。在'StateServer'和'SQLServer'模式下,ASP.NET會序列化會話狀態對象,不允許使用不可序列化的對象或MarshalByRef對象。如果自定義會話狀態存儲庫在「自定義」模式下完成類似的序列化,則應用相同的限制。

我假設這是因爲我在頁面上使用的分頁代碼。該代碼如下:

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; 
    } 
} 

我只想知道在編碼和「如何序列化會話」是什麼問題? 我做什麼來改善編碼?

任何幫助將不勝感激。

謝謝

+0

它是否因爲我在會議中使用大量會話進行分頁?我應該使用其他代碼進行尋找嗎?或者我應該修改如何使所有會話對象可串行化? – jackerj 2013-03-13 10:28:32

+3

停止使用CapsLock我們不是盲目的 – 2013-03-13 10:47:02

+0

嘗試在發佈重複問題之前下次使用搜索。請檢查此:http://stackoverflow.com/questions/10230880/serializing-session-state-in-asp-net – 2013-03-13 10:53:34

回答

1

我想你的自定義分頁控制是不可序列化的。這一定是問題的原因。

無論如何,在會話中存儲控件並不是一個好主意。只需存儲幾個可重新構建控件的可序列化屬性(PageSize和CurrentPageIndex),或者將它們傳遞到查詢字符串中作爲示例。

你也可以使用ViewState的,如果你能

關於在會話中存儲數據表,這可能,如果你有大量的數據和許多連接的用戶是一個非常糟糕的主意。

+0

你能告訴我一些編碼嗎? – jackerj 2013-03-13 12:41:20

+0

在會話中存儲int值是基本的會話處理。關於viewstate,看看http://msdn.microsoft.com/en-us/library/ms227551%28v=vs.85%29.aspx – jbl 2013-03-13 13:01:30

相關問題