0

I want to display the SharePoint List Items on the page based on the filter applied.

Managed to get it done with SPGridView however I see that it can be improved.

  1. As paging is enabled, it reloads the Items every time.
  2. Columns are getting added on page load again and again

any way I could improve this?

public partial class MediaCenterListViewUserControl : UserControl
{
    private MediaCenterListView _webpart = null;

    internal void ConnectWebPart(MediaCenterListView webpart)
    {
        _webpart = webpart;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ItemsGridView.AllowPaging = true;
        ItemsGridView.PagerTemplate = null;
        ItemsGridView.PageSize = 10;
        ItemsGridView.PageIndexChanging += new GridViewPageEventHandler(ItemsGridView_PageIndexChanging);

        objectDataSource = new ObjectDataSource(this.GetType().AssemblyQualifiedName, "LoadListItems");
        objectDataSource.ObjectCreating += new ObjectDataSourceObjectEventHandler(objectDataSource_ObjectCreating);
    }

    protected override void OnPreRender(EventArgs e)
    {
        var web = SPContext.Current.Web;
        var spList = web.Lists[_webpart.ListName];

        if (ItemsGridView.Columns.Count == 0)
        {
            ItemsGridView.Columns.Add(new HyperLinkField
            {
                DataTextField = "ID",
                HeaderText = "ID",
                DataNavigateUrlFields = new string[] { "ID" },
                DataNavigateUrlFormatString = string.Format("{0}{1}{2}", web.Url, spList.DefaultEditFormUrl, "?ID={0}"),
                Target = "_blank"
            });
            ItemsGridView.Columns.Add(new SPBoundField { DataField = "LinkFilename", HeaderText = "Name" });
            ItemsGridView.Columns.Add(new SPBoundField { DataField = "ImageCategory", HeaderText = "Image Category" });
        }

        try
        {
            this.BindItems();
        }
        catch (Exception ex)
        {
            SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local;
            SPDiagnosticsCategory cat = diagnosticsService.Areas["SharePoint Foundation"].Categories["Unknown"];
            diagnosticsService.WriteTrace(1, cat, TraceSeverity.Medium, ex.StackTrace, cat.Name, cat.Area.Name);
        }
    }

    private void BindItems()
    {
        ItemsGridView.DataSource = objectDataSource;
        ItemsGridView.DataBind();
    }

    public DataTable LoadListItems()
    {
        var web = SPContext.Current.Web;
        var spList = web.Lists[_webpart.ListName];
        var spListItems = spList.Items.GetDataTable();

        return spListItems;
    }

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

    void ItemsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ItemsGridView.PageIndex = e.NewPageIndex;
    }
}
1

You could improve it by getting rid of the SPGridview and going to a client side approach - maybe backed by a custom web service if you need server code. The SPGridview is a server control and is going to require a trip to the server.

I suppose you might be able to use an UpdatePanel, if you really need to use the GridView and have a desire to punish yourself.

  • I would like to get the job done without too much effort, js based solution may require additional effort. Can you please let me know how to use update panel with spgridview. – Karthikeyan Feb 24 '18 at 21:46
  • google.com/… – Derek Gusoff Feb 25 '18 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.