Sharepoint 2010沙盒Web部件错误

时间:2011-07-19 07:29:21

标签: sharepoint-2010 web-parts

我是sharepoint的新手,我在sharepoint 2010中创建了一个webpart,我在部署webpart时添加了一个SPGridview,它正在生成此异常

  

Web部件错误:部分信任应用程序域中沙盒代码包装器的Execute方法抛出了未处理的异常:发生了意外错误。

代码就像这样

    using System;
using System.ComponentModel;
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;
using System.Data;

namespace DemoProject.HelloWorldWebPart
{
    [ToolboxItemAttribute(false)]
    public class HelloWorldWebPart : WebPart
    {
        protected override void CreateChildControls()
        {
            //Label lblHelloWorld = new Label();
            //lblHelloWorld.Text = "Hello World";
            //this.Controls.Add(lblHelloWorld);
            //const string DATASOURCEID = "gridDS";

            ObjectDataSource gridDS = new ObjectDataSource();
            gridDS.ID = "gridDS";
            gridDS.SelectMethod = "SelectData";
            gridDS.TypeName = this.GetType().AssemblyQualifiedName;
            gridDS.ObjectCreating += new ObjectDataSourceObjectEventHandler(gridDS_ObjectCreating);
            this.Controls.Add(gridDS);

            // Instantiating Sharepoint GridView object and assigning properties to it
            SPGridView spgvStudentList = new SPGridView();
            spgvStudentList.ID = "spgvStudentList";
            spgvStudentList.DataSourceID = gridDS.ID;
            spgvStudentList.AutoGenerateColumns = false;
            spgvStudentList.AllowPaging = true;
            spgvStudentList.PageSize = 5;
            spgvStudentList.AllowSorting = true;
            this.Controls.Add(spgvStudentList);

            SPGridViewPager pager = new SPGridViewPager();
            pager.GridViewId = spgvStudentList.ID;
            this.Controls.Add(pager);
        }
        public DataTable SelectData()
        {
            // Creating a datasource object for databinding the sharepoint gridview
            DataTable dataSource = new DataTable();

            dataSource.Columns.Add("ID");
            dataSource.Columns.Add("Name");
            dataSource.Columns.Add("Region");
            dataSource.Columns.Add("Total Sales");

            dataSource.Rows.Add(1, "J. Smith", "Europe", 10000);
            dataSource.Rows.Add(2, "J. Smith", "North America", 15000);
            dataSource.Rows.Add(3, "J. Smith", "Asia", 5000);
            dataSource.Rows.Add(4, "S. Jones", "Europe", 7000);
            dataSource.Rows.Add(5, "S. Jones", "North America", 30000);
            dataSource.Rows.Add(6, "S. Jones", "Asia", 8700);
            dataSource.Rows.Add(7, "W. Nguyen", "Europe", 3000);
            dataSource.Rows.Add(8, "W. Nguyen", "North America", 50000);
            dataSource.Rows.Add(9, "W. Nguyen", "Asia", 25000);

            return dataSource;
        }

        private void gridDS_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = this;
        }
    }
}

任何人都可以告诉我这里我做错了什么吗? 感谢致敬 MAC

1 个答案:

答案 0 :(得分:0)

我得到了它我使用了sanboxed解决方案,来自Sandboxed工作进程的调用仅限于使用几个类和方法。不幸的是,Microsoft.Sharepoint.WebControls也是其中一个类。我们仅限于使用ASP.Net控件。我把它改成了Farm Solutions,它工作正常。