0

请有人给我一个解释。我有一个显示表格和分页的网络表单。表格和分页由 Web 表单中包含的 Web 控制器执行。当我将项目从 Web 表单传递到 Web 控件时,一切都很好,但是当我修改 Web 控件中的值并尝试将它们绑定到转发器时,我得到了错误。

我已经初始化并且传递的值不为空,我从调试中知道。

public class Table : System.Web.UI.UserControl, IPageableControl
{
    // PagedDataSource encapsulates the properties needed to enable paging for the control
    PagedDataSource page = new PagedDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            object o = Object;
            Session["o"] = o;
        }
    }

    public object Target
    {
        get { return m_ItemsRepeater.DataSource; }
        set
        {
            if (value == null)
                return;

            // display the table of items
            m_ItemsRepeater.DataSource = value;  // here I get the error after clicking
            m_ItemsRepeater.DataBind();
        }
    }

    // when I click on next or prev another controller is called which passes a value to Page
    public void Page(int i)
    {

        // if i == 1 go to next page if -1 to prev page
        if (i == 1)
            CurrentPage += CurrentPage;
        else
        {
            CurrentPage -= CurrentPage;                
        }

        SetPage();
    }

    public void SetPage()
    {
        object oo = (object)System.Web.HttpContext.Current.Session["o"];

        List<USER> u = (List<USER>)oo;

        // number of items per page
        int iPageSize = 5;
        // total items (=> total number of items)
        int total_number_items = u.Count;

        // current page ranges from 0 to max_number_pages-1
        int iTotalPages = (int)Math.Ceiling(((double)total_number_items) / iPageSize);

        // Indicate that the data should be paged              
        page.AllowPaging = true;
        // Set the number of items you wish to display per page
        page.PageSize = iPageSize;

        // number of already displayed items
        int index = CurrentPage * iPageSize;

        // select items to display in the current page. Check if out of range
        if (index + iPageSize < total_number_items)
            page.DataSource = u.GetRange(index, iPageSize).ToList();
        else
            page.DataSource = u.GetRange(index, total_number_items - index).ToList();

        // display items in the pages
        Target = page;

    }

    object s;
    public object Object
    {
        get { return s; }
        set { s = value; }
    }

    public int CurrentPage
    {
        get
        {
            object obj = this.ViewState["CurrentPage"];

            if (obj == null)
            {
                return 0;
            }
            else
            {
                return (int)obj;
            }
        }
        set
        {
            this.ViewState["CurrentPage"] = value;
        }
    }

}

// Web form

public class Items
{
    PagedDataSource page = new PagedDataSource();
    // number of items per page
    int iPageSize = 5;
    int total_number_items;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // Load the items
            LoadItems();
        }
        else
        {
            // load session using paging from webcontrol
            List<USER> items = db.Get();
            m_Table.Object = items;
        }
    }

    /// <summary>
    /// Load items and display in table.
    /// </summary>
    private void LoadItems()
    {
        // get list items
        List<USER> items = db.Get();


        DataBind(items, CurrentPage);
    }

    /// <summary>
    /// Enable paging.
    /// </summary>
    private void DataBind(List<USER> items, int current_page)
    {
        // Indicate that the data should be paged
        page.AllowPaging = true;
        // Set the number of items you wish to display per page
        page.PageSize = iPageSize;

        // number of total pages
        int iTotalPages = ((items.Count + iPageSize - 1) / iPageSize);
        // number of already displayed items
        int index = CurrentPage * iPageSize;

        // select items to display in the current page. Check if out of range
        if (index + iPageSize < items.Count)
            page.DataSource = items.GetRange(index, iPageSize).ToList();
        else
            page.DataSource = items.GetRange(index, items.Count - index).ToList();

        // display items in the pages
        m_Table.Target = page;

    }

    // Called when cursor Previous is clicked.
    protected void lnkPrevious_Click(object sender, EventArgs e)
    {
        // Set viewstate variable to the previous page
        CurrentPage -= 1;
        LoadSupporters();
    }

    // Called when cursor Next is clicked.
    protected void lnkNext_Click(object sender, EventArgs e)
    {
        // Set viewstate variable to the next page
        CurrentPage += 1;
        LoadSupporters();
    }
    public int CurrentPage
    {
        get
        {
            object obj = this.ViewState["CurrentPage"];

            if (obj == null)
            {
                return 0;
            }
            else
            {
                return (int)obj;
            }
        }
        set
        {
            this.ViewState["CurrentPage"] = value;
        }
    }



}
4

1 回答 1

0

检查这object s没有分配并像这样改变

object s;
public object Object
{
    get { return s == null ? new object() : s; }
    set { s = value; }
}
于 2013-02-01T10:45:57.083 回答