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



这是我的第一个sharepoint项目,每个项目看起来都很混乱。

我需要一个带有分页功能的SPGridView。

以下是mx Web部件的完整代码:

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)rn   at Microsoft.SharePoint.WebControls.Menu.FindControlByWalking(Control namingContainer, String id)rn   at Microsoft.SharePoint.WebControls.SPGridViewPager.get_GridViewControl()rn   at Microsoft.SharePoint.WebControls.SPGridViewPager.Render(HtmlTextWriter output)rn   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)rn   at System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)rn   at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)rn   at FirstSPGridView.VisualWebPart1.VisualWebPart1.Render(HtmlTextWriter writer)

我该如何修复该错误?

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

相关内容

  • 没有找到相关文章

最新更新