简体   繁体   中英

Object reference not set to an instance of an object, error message returned from a webcontrol

Please could someone give me an explanation. I have a webform which displays a table and paging. Table and paging are performed by a web controller included in the web form. When I pass the items from web form to web control everything is fine, but when I modify the values in web controll and try to bind them to the repeater I obtain the error.

I have initialezed and the passed value is not null, I know it from debug.

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



}

check this object s is not assigned and change like this

object s;
public object Object
{
    get { return s == null ? new object() : s; }
    set { s = value; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM