使用SPGridViewPager时,对象引用未设置为对象的实例

时间:2012-03-01 21:20:04

标签: sharepoint-2010 spgridview

这是我的第一个sharepoint项目,并且eveything看起来很混乱。

我需要一个带分页的SPGridView。

以下是mx webpart的完整代码:

using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace FirstSPGridView.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    public class VisualWebPart1 : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/FirstSPGridView/SPGridViewWebPartTest/VisualWebPart1UserControl.ascx";

        SPGridView _grid;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            try
            {
                SPSite mySite = SPContext.Current.Site;
                SPWeb myWeb = mySite.OpenWeb();

                //Using RunWithElevatedPrivileges

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite siteCollection = new SPSite(mySite.ID))
                    {
                        using (SPWeb web = siteCollection.OpenWeb(myWeb.ID))
                        {
                            _grid = new SPGridView();
                            _grid.AutoGenerateColumns = false;


                            _grid.PageSize = 3;
                            _grid.AllowPaging = true;

                            _grid.DataSource = SelectData();


                            Controls.Add(_grid);

                            SPGridViewPager pager = new SPGridViewPager();
                            pager.GridViewId = _grid.ID;

                            this.Controls.Add(pager);

                        }
                    }
                });
            }
            catch (Exception ex)
            { }


        }

        protected sealed override void Render(HtmlTextWriter writer)
        {
            try
            {
                GenerateColumns();
                _grid.DataBind();
                base.Render(writer);
            }
            catch (Exception e)
            {
                throw new NotImplementedException();
            }
        }

        private void GenerateColumns()
        {
            BoundField clientNameColumn = new BoundField();
            clientNameColumn.HeaderText = "Client";
            clientNameColumn.DataField = "LastName";
            _grid.Columns.Add(clientNameColumn);

            BoundField birthDayColumn = new BoundField();
            birthDayColumn.HeaderText = "BirthDate";
            birthDayColumn.DataField = "BirthDate";

            _grid.Columns.Add(birthDayColumn);
        }


        public DataTable SelectData()
        {
            var dataGet = new DataTable();
            SPSecurity.RunWithElevatedPrivileges(delegate()
                                                     {
                                                         using (
                                                             var conn =
                                                                 new SqlConnection(
                                                                     "Data Source=localhost;Initial Catalog=AdventureWorksDW2008R2;Integrated Security=True")
                                                             )
                                                         {
                                                             var adapter = new SqlDataAdapter();
                                                             adapter.SelectCommand =
                                                                 new SqlCommand("Select TOP 100 LastName,Birthdate FROM DimCustomer");
                                                             adapter.SelectCommand.Connection = conn;
                                                             conn.Open();

                                                             adapter.Fill(dataGet);

                                                         }
                                                     });
            return dataGet;


        }
    }
}

一切正常(除了分页,直到我添加了这段代码:

SPGridViewPager pager = new SPGridViewPager();
                            pager.GridViewId = _grid.ID;

                            this.Controls.Add(pager);

之后我在Render方法上得到一个例外:

                base.Render(writer);

StackTrace是:

   at System.Web.UI.Control.FindControl(String id, Int32 pathOffset)\r\n   at Microsoft.SharePoint.WebControls.Menu.FindControlByWalking(Control namingContainer, String id)\r\n   at Microsoft.SharePoint.WebControls.SPGridViewPager.get_GridViewControl()\r\n   at Microsoft.SharePoint.WebControls.SPGridViewPager.Render(HtmlTextWriter output)\r\n   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)\r\n   at System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)\r\n   at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)\r\n   at FirstSPGridView.VisualWebPart1.VisualWebPart1.Render(HtmlTextWriter writer)

如何修复该错误?

1 个答案:

答案 0 :(得分:0)

您必须只为_grid设置ID。 例如,_grid.ID = "_gridView";