2012-03-01 46 views
0

這是我的第一個共享點項目,eveything看起來很混亂。使用SPGridViewPager時未將對象引用設置爲對象的實例

我需要一個帶分頁功能的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); 

堆棧跟蹤是:

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

值得檢查的事情:仔細檢查CreateChildControls是否在Render之前被調用。另外,如果將base.CreateChildControls移動到代碼後面會發生什麼情況。 – EtherDragon 2012-03-01 23:35:38

+0

感謝@EtherDragon的評論。我發現了另一個解決方案:http://mossindepth.blogspot.com/2009/05/spgridview-grouping-is-it-easy-to-apply.html,它的工作方式應該如何 – user278618 2012-03-04 19:14:07

回答

0

您必須爲_grid設置ID。 例如,_grid.ID = "_gridView";

相關問題