【问题标题】:How to get Filtering in SPGridView working如何在 SPGridView 中进行过滤
【发布时间】:2011-07-06 20:35:47
【问题描述】:

我在自定义 Web 部件上设置了过滤代码,但在选择过滤时网站上出现错误,我尝试了一些可能的修复方法,但无济于事,并寻求一些关于我可能遗漏的帮助/做错了。

代码..

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

namespace Test.TestWebPart
{
    public partial class TestWebPartUserControl : UserControl
    {
        //Global variable call
        private SPSite thisSite = SPContext.Current.Site;
        //private SPWebCollection thisWeb;//
        private SPWeb thisWeb = SPContext.Current.Web;
        private DataTable dt;
        private SPListCollection siteLists;
        private DataTableWrapper myDataTable;



        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                BindToGrid();
            }
        }

        private void BindToGrid()
        {
            dt = new DataTable();
            dt.Columns.Add("Title");
            dt.Columns.Add("Created");
            dt.Columns.Add("List");
            dt = SelectData();

            myDataTable = new DataTableWrapper(dt);
            Type t = myDataTable.GetType();


            ObjectDataSource ds = new ObjectDataSource();
            ds.ID = "myDataSource";
            ds.TypeName = t.AssemblyQualifiedName;
            ds.SelectMethod = "GetTable";
            ds.ObjectCreating += new ObjectDataSourceObjectEventHandler(ds_ObjectCreating);
            this.Controls.Add(ds);

            grid.ID = "gridID";


            //Bind the three columns to the SPGridView
            //HtmlEncode must be false for the links to appear as true html
            BoundField column = new BoundField();
            column.DataField = "Title";
            column.HtmlEncode = false;
            //column.SortExpression = "Title";
            column.HeaderText = "Title";
            grid.Columns.Add(column);

            BoundField column1 = new BoundField();
            column1.DataField = "Created";
            column1.HtmlEncode = true;
            //column1.SortExpression = "Created";
            column1.HeaderText = "Created";
            grid.Columns.Add(column1);

            BoundField column2 = new BoundField();
            column2.DataField = "List";
            column2.HtmlEncode = false;
            //column2.SortExpression = "List";
            column2.HeaderText = "List";
            grid.Columns.Add(column2);


            grid.AllowFiltering = true;
            grid.FilterDataFields = "Title,Created,ListName";
            grid.FilteredDataSourcePropertyName = "FilterExpression";
            grid.FilteredDataSourcePropertyFormat = "{1} == '{0}'";


            //Provide the SPGridview with the DataSource
            grid.DataSourceID = "myDataSource";
            this.Controls.Add(grid);



            //Default Pagination - commented out due to not working
            //grid.PageIndexChanging += new GridViewPageEventHandler(grid_PageIndexChanging);
            //grid.PagerTemplate = null;

            //Bind the data to the grid
            grid.DataBind();


        }

        //private void GenerateColumns()
        //{

        //}

        //Used to deal with the PageIndexChange event
        void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grid.PageIndex = e.NewPageIndex;
            grid.DataBind();
        }

        //Used to deal with the ObjectCreated event
        void ds_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            myDataTable = new DataTableWrapper(dt);
            e.ObjectInstance = myDataTable;
        }

        //Pulls the data from lists which will be displayed
        public DataTable SelectData()
        {
            try
            {
                //Create a new instance of type DataRow
                DataRow row;

                //Loop through each website in the webcollection

                {
                    //Pull the lists from the site into a list collection
                    siteLists = thisWeb.Lists;
                    //Display only lists the current user has access to
                    siteLists.ListsForCurrentUser = true;

                    SPBasePermissions perms = SPBasePermissions.ViewListItems;

                    //Loop through each list within the list collection
                    foreach (SPList list in siteLists)
                    {
                        if (list.DoesUserHavePermissions(perms))
                        {
                            //If the list is an announcement list continue otherwise skip
                            if (list.BaseTemplate.ToString() == "Announcements")
                            {
                                //Exclude the lists stated from those whose data will be collected
                                if (list.Title.ToString() == "The Buzz" || list.Title.ToString() == "Test 2 list")
                                {
                                }
                                else
                                {
                                    //Create a item collection for each item within the current list
                                    SPListItemCollection listItem = list.Items;

                                    //Loop through each item within the item collection
                                    foreach (SPListItem item in listItem)
                                    {
                                        //Get the url of the current website
                                        string weburl = thisWeb.Url;
                                        //Gets the URL of the current item
                                        string dispurl = item.ContentType.DisplayFormUrl;
                                        dispurl = list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;

                                        //Joins together the full URL for the current item into a single variable
                                        dispurl = string.Format("{0}/{1}?ID={2}", weburl, dispurl, item.ID);
                                        //Create a new in the datatable as an instance of row
                                        row = dt.Rows.Add();

                                        //Put the correct information and links into the correct column
                                        row["Title"] = "<a target=_blank href=\"" + dispurl + "\">" + item["Title"].ToString() + "</a>";
                                        row["Created"] = item["Created"].ToString();
                                        row["List"] = "<a target=_blank href=\"" + list.DefaultViewUrl + "\">" + list.Title + "</a>";
                                    }
                                }
                            }
                        }
                    }
                }
                //Return the completed DataTable
                return dt;
            }

            //Exception to catch any errors
            catch (Exception s)
            {
                return dt;
            }
        }
    }
}

网站上的错误:-

错误:调用了 SPGridView_FilterCallbackErrorHandler() - 结果=找不到回调的目标 'ctl00$m$g_f0816b70_5f1d_4c59_9ba2_39401a4d7ea6$ctl00$gridID' 或未实现 ICallbackEventHandler....

任何帮助表示赞赏。谢谢。

【问题讨论】:

    标签: sharepoint-2010 web-parts


    【解决方案1】:

    将分配网格 ID 的行移动到每次页面加载时调用的位置

    protected void Page_Load(object sender, EventArgs e) {
        grid.ID = "gridID";
        if (!Page.IsPostBack) {
            BindToGrid();
        }
    }
    

    编辑

    实际上,您需要移动创建控件树的所有代码,以便在每次页面加载时都构建它。最好在 OnInit 或 CreateChildControls 中执行此操作。像

    this.Controls.Add(xyz);
    

    【讨论】:

    • 感谢您的建议,不完全确定添加此内容的最佳方式,感谢任何帮助/建议。
    【解决方案2】:

    djeeg 是对的。您需要确保所有控件都加载到 CreateChildControls 中。对我来说,我必须像这样加载和设置覆盖中的所有属性。

    protected override void CreateChildControls()
    {
    
         InitGridView();
    
         InitBoundColumns();
    
         InitDataSource();
    
         ... //other code ellided
    
         Controls.Add(_scopingGridView);
    
     }
    

    使用上述每个方法分别执行 SPGridView、BoundColumsn 和 ObjectDataSource 的所有设置。希望这可以帮助。有关我如何使用上述内容的其他信息,请参阅http://www.threewill.com/2010/08/the-great-and-powerful-spgridview/...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      相关资源
      最近更新 更多