0

I have a custom GridView control that has pagination built in it. I've used this component many times on Sharepoint 2010 without a problem. Now i'm trying to use it on a Sharepoint 2013 Web Part solution just as i did on my previous 2010 solutions and it's giving me that "The name 'InitializeControl' does not exist in the current context" error.

After i declare the component on my ascx, the .g.cs file goes empty and the error show up.

Here's my ascx control

<%@ Register Assembly="Conectt.Componentes.Noticias, Version=1.0.0.0, Culture=neutral, PublicKeyToken=73b1d20ea6c5bde8"
Namespace="Conectt.Componentes.Noticias" TagPrefix="Conectt" %>
<Conectt:PaginatedGrid ID="gridListagemNoticias" runat="server" OnRowDataBound="gridListagemNoticias_RowDataBound"
    OnPageIndexChanging="gridListagemNoticias_PageIndexChanging" AutoGenerateColumns="false"
    AllowPaging="true" UseCustomPager="true" ShowHeader="false" ShowFooter="false"
    RowStyle-CssClass="paginatedGridItem" AlternatingRowStyle-CssClass="paginatedGridAlternatingItem"
    ShowFirstLastArrows="true" ShowNavigationArrows="false" AllowSorting="false">
    <Columns>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <ul>
                            <li><asp:Label ID="lblName" runat="server" /></li>
                            <li><asp:Label ID="lblPhone" runat="server" /></li>
                        </ul>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
</Conectt:PaginatedGrid>

And here s my paginatedgrid code

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
[assembly: TagPrefix("Conectt.Componentes.Noticias", "Conectt")]
namespace Conectt.Componentes.Noticias
{
    public class PaginatedGrid : GridView
    {
        public bool UseCustomPager
        {
            get
            {
                if (ViewState["UseCustomPager"] != null)
                    return Convert.ToBoolean(ViewState["UseCustomPager"]);
                else
                    return true;
            }

            set { ViewState["UseCustomPager"] = value; }
        }

        public bool ShowNavigationArrows
        {
            get
            {
                if (ViewState["ShowNavigationArrows"] != null)
                    return Convert.ToBoolean(ViewState["ShowNavigationArrows"]);
                else
                    return true;
            }

            set { ViewState["ShowNavigationArrows"] = value; }
        }
        public bool ShowFirstLastArrows
        {
            get
            {
                if (ViewState["ShowFirstLastArrows"] != null)
                    return Convert.ToBoolean(ViewState["ShowFirstLastArrows"]);
                else
                    return false;
            }

            set { ViewState["ShowFirstLastArrows"] = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.CellPadding = 0;
            this.CellSpacing = 0;
            this.BorderWidth = 0;

            if (string.IsNullOrEmpty(this.CssClass))
                this.CssClass = "paginatedGrid";

            if (this.Width.IsEmpty)
                this.Width = new Unit("100%");

            if (string.IsNullOrEmpty(this.HeaderStyle.CssClass))
                this.HeaderStyle.CssClass = "paginatedGridHeader";

            if (string.IsNullOrEmpty(this.RowStyle.CssClass))
                this.RowStyle.CssClass = "paginatedGridItem";

            if (string.IsNullOrEmpty(this.AlternatingRowStyle.CssClass))
                this.AlternatingRowStyle.CssClass = "paginatedGridAlternatingItem";

            if (string.IsNullOrEmpty(this.PagerStyle.CssClass))
                this.PagerStyle.CssClass = "paginatedGridPager";

            if (string.IsNullOrEmpty(this.EmptyDataText))
                this.EmptyDataText = "Nenhum registro encontrado.";

            if (this.EmptyDataTemplate == null)
                this.EmptyDataTemplate = new GridViewTemplate();
        }

        protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
        {
            if (UseCustomPager)
                CreateCustomPager(row, columnSpan, pagedDataSource);
            else
                base.InitializePager(row, columnSpan, pagedDataSource);
        }

        protected virtual void CreateCustomPager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
        {
            int pageCount = pagedDataSource.PageCount;
            int pageIndex = pagedDataSource.CurrentPageIndex + 1;
            int pageButtonCount = 3;

            TableCell cell = new TableCell();
            row.Cells.Add(cell);
            if (columnSpan > 1) cell.ColumnSpan = columnSpan;

            if (pageCount > 1)
            {
                int min = pageIndex - pageButtonCount;
                int max = pageIndex + pageButtonCount;

                if (max > pageCount)
                    min -= max - pageCount;
                else if (min < 1)
                    max += 1 - min;

                HtmlGenericControl divContainer = new HtmlGenericControl("div");
                divContainer.Attributes.Add("class", "paginatedGridPagesContainer");

                #region Create "First Page" and "Previous Page" button
                if (pageIndex > 1 && ShowFirstLastArrows)
                {
                    // create "first page" button
                    //cell.Controls.Add(BuildLinkButton(0, PagerButtonDirection.Prev, true, "Page", "First"));
                    divContainer.Controls.Add(BuildImageButton(0, PagerButtonDirection.Prev, "Page", "First"));
                }

                if (pageIndex > 1 && ShowNavigationArrows)
                {
                    // create "previous page" button
                    //cell.Controls.Add(BuildImageButton(0, PagerButtonDirection.Prev, "Page", "Previous"));
                    divContainer.Controls.Add(BuildImageButton(0, PagerButtonDirection.Prev, "Page", "Previous"));
                }
                #endregion

                #region Create page buttons

                bool needDiv = false;
                for (int i = 1; i <= pageCount; i++)
                {
                    Control page;

                    if (i <= 2 || i > pageCount - 2 || (min <= i && i <= max))
                    {
                        string text = i.ToString(NumberFormatInfo.InvariantInfo);
                        page = i == pageIndex
                                ? BuildTextSpan(text, "paginatedGridCurrentPage")
                                : BuildLinkButton(i - 1, text, "Page", text);
                        //cell.Controls.Add(page);
                        divContainer.Controls.Add(page);
                        needDiv = true;
                    }
                    else if (needDiv)
                    {
                        //cell.Controls.RemoveAt(cell.Controls.Count - 1);
                        divContainer.Controls.RemoveAt(divContainer.Controls.Count - 1);

                        page = BuildTextSpan("&hellip;", null);
                        divContainer.Controls.Add(page);
                        //cell.Controls.Add(page);
                        needDiv = false;
                    }
                }

                #endregion

                #region Create "Next Page" and "Last Page" button
                if (pageIndex < pageCount && ShowNavigationArrows)
                {
                    // create "next page" button
                    //cell.Controls.Add(BuildImageButton(pageCount, PagerButtonDirection.Next, "Page", "Next"));
                    divContainer.Controls.Add(BuildImageButton(pageCount, PagerButtonDirection.Next, "Page", "Next"));
                }

                if (pageIndex < pageCount && ShowFirstLastArrows)
                {
                    // create "last page" button
                    //cell.Controls.Add(BuildLinkButton(pageCount, PagerButtonDirection.Next, true, "Page", "Last"));
                    divContainer.Controls.Add(BuildImageButton(pageCount, PagerButtonDirection.Next, "Page", "Last"));
                }
                #endregion

                cell.Controls.Add(divContainer);
            }
        }

        #region Link Buttons
        private Control BuildLinkButton(int pageIndex, PagerButtonDirection dir, Boolean isFirstLast, string commandName, string commandArgument)
        {
            PagerLinkButton btn = new PagerLinkButton(this);
            if (isFirstLast)
                btn.ToolTip = btn.Text = dir == PagerButtonDirection.Prev ? "Primeira Página" : "Última Página";
            else
                btn.ToolTip = btn.Text = dir == PagerButtonDirection.Prev ? "Voltar Página" : "Avançar Página";
            btn.EnableCallback(ParentBuildCallbackArgument(pageIndex));
            btn.CommandName = commandName;
            btn.CommandArgument = commandArgument;
            return btn;
        }

        private Control BuildLinkButton(int pageIndex, string text, string commandName, string commandArgument)
        {
            PagerLinkButton btn = new PagerLinkButton(this);
            btn.Text = text;
            btn.ToolTip = text;
            btn.CssClass = "paginatedGridInactivePage";
            btn.EnableCallback(ParentBuildCallbackArgument(pageIndex));
            btn.CommandName = commandName;
            btn.CommandArgument = commandArgument;
            return btn;
        }

        private Control BuildTextSpan(string text, string cssClass)
        {
            HtmlGenericControl span = new HtmlGenericControl("span");
            if (!String.IsNullOrEmpty(cssClass)) span.Attributes["class"] = cssClass;
            span.InnerHtml = text;
            return span;
        }
        #endregion

        #region Image Buttons
        private Control BuildImageButton(int pageIndex, PagerButtonDirection dir, string commandName, string commandArgument)
        {
            PagerImageButton btn = new PagerImageButton(this);
            btn.ImageUrl = dir == PagerButtonDirection.Prev ? "/_layouts/Images/Conectt.Componentes.BuscaRamais/PaginatedGrid/paginacaoVoltarPagina.png" : "/_layouts/Images/Conectt.Componentes.BuscaRamais/PaginatedGrid/paginacaoAvancarPagina.png";
            btn.Width = 28;
            btn.Height = 28;
            btn.AlternateText = dir == PagerButtonDirection.Prev ? "Primeira Página" : "Última Página";
            btn.ToolTip = btn.AlternateText;
            btn.EnableCallback(ParentBuildCallbackArgument(pageIndex));
            btn.CommandName = commandName;
            btn.CommandArgument = commandArgument;
            return btn;
        }

        private Control BuildImageButton(int pageIndex, string text, string commandName, string commandArgument)
        {
            PagerImageButton img = new PagerImageButton(this);
            img.ImageUrl = "/_layouts/Images/Conectt.Componentes.BuscaRamais/PaginatedGrid/paginacaoMarcadorPaginaNaoAtiva.png";
            img.AlternateText = text;
            img.ToolTip = text;
            img.EnableCallback(ParentBuildCallbackArgument(pageIndex));
            img.CommandName = commandName;
            img.CommandArgument = commandArgument;
            return img;
        }

        private Control BuildImageSpan(string text, string cssClass)
        {
            HtmlGenericControl span = new HtmlGenericControl("span");
            if (!String.IsNullOrEmpty(cssClass)) span.Attributes["class"] = cssClass;
            span.InnerHtml = "<img src='/_layouts/Images/Conectt.Componentes.BuscaRamais/PaginatedGrid/paginacaoMarcadorPaginaAtiva.png' alt='" + text + "' title='" + text + "' />";
            return span;
        }
        #endregion

        private string ParentBuildCallbackArgument(int pageIndex)
        {
            MethodInfo m =
                typeof(GridView).GetMethod("BuildCallbackArgument", BindingFlags.NonPublic | BindingFlags.Instance, null,
                                            new Type[] { typeof(int) }, null);
            return (string)m.Invoke(this, new object[] { pageIndex });
        }

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.ShowHeader && this.Rows.Count > 0)
                writer.WriteLine("<div class='paginatedGridDestacaTitulo'>");

            base.Render(writer);

            if (this.ShowHeader && this.Rows.Count > 0)
                writer.WriteLine("</div>");
        }
    }

    public class GridViewTemplate : ITemplate
    {
        void ITemplate.InstantiateIn(System.Web.UI.Control container)
        {
            Literal lit = new Literal();
            lit.Text = "<div class='PaginatedGridEmpty'>Nenhum registro encontrado.</div>";
            container.Controls.Add(lit);
        }
    }

    enum PagerButtonDirection
    {
        Prev,
        Next
    }

    public class PagerImageButton : ImageButton
    {
        public PagerImageButton(IPostBackContainer container)
        {
            _container = container;
        }

        public void EnableCallback(string argument)
        {
            _enableCallback = true;
            _callbackArgument = argument;
        }

        public override bool CausesValidation
        {
            get { return false; }
            set { throw new ApplicationException("Cannot set validation on pager buttons"); }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            SetCallbackProperties();
            base.Render(writer);
        }

        private void SetCallbackProperties()
        {
            if (_enableCallback)
            {
                ICallbackContainer container = _container as ICallbackContainer;
                if (container != null)
                {
                    string callbackScript = container.GetCallbackScript(this, _callbackArgument);
                    if (!string.IsNullOrEmpty(callbackScript)) OnClientClick = callbackScript;
                }
            }
        }

        #region Private fields

        private readonly IPostBackContainer _container;
        private bool _enableCallback;
        private string _callbackArgument;

        #endregion
    }

    public class PagerLinkButton : LinkButton
    {
        public PagerLinkButton(IPostBackContainer container)
        {
            _container = container;
        }

        public void EnableCallback(string argument)
        {
            _enableCallback = true;
            _callbackArgument = argument;
        }

        public override bool CausesValidation
        {
            get { return false; }
            set { throw new ApplicationException("Cannot set validation on pager buttons"); }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            SetCallbackProperties();
            base.Render(writer);
        }

        private void SetCallbackProperties()
        {
            if (_enableCallback)
            {
                ICallbackContainer container = _container as ICallbackContainer;
                if (container != null)
                {
                    string callbackScript = container.GetCallbackScript(this, _callbackArgument);
                    if (!string.IsNullOrEmpty(callbackScript)) OnClientClick = callbackScript;
                }
            }
        }

        #region Private fields

        private readonly IPostBackContainer _container;
        private bool _enableCallback;
        private string _callbackArgument;

        #endregion
    }
}

I'm not sure if it's something i'm missing on how i'm declaring the control on the page or what. Any clues on what could be happening?

Thanks a lot.

1 Answer 1

0

Solved my problems using the SharePoint OTB SPGridView

http://code.msdn.microsoft.com/office/Ejemplos-sobre-cmo-usar-el-69cd5f16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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