custom paging using 'next/pre with numeric'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pupilstuff
    New Member
    • Jun 2009
    • 18

    custom paging using 'next/pre with numeric'

    hi guys i just want to perform custom paging in which at the footer of the grid view ,there must be a pager 'pervious/next with numeric'

    this is what i did

    in aspx page
    Code:
      <asp:GridView ID="TableGridView"  
          
          OnPageIndexChanging="TableGridView_PageIndexChanging"      
          runat="server"  AutoGenerateColumns="False" 
         AllowPaging="True" AllowSorting="True" >
        
     </asp:GridView>
          
            <asp:Button ID="btnConnect" runat="server"  Style="z-index: 113;
                left: 260px; position: absolute; top: 143px" Text="Connect & Populate" OnClick="btnConnect_Click" />
    in code behind page
    Code:
    public partial class _Default : System.Web.UI.Page
    {
        
        public static DataTable Table = new DataTable();
        ArrayList ParameterArray = new ArrayList();
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack && (bool)Session["IsConnectionInfoSet"]==true)
                CreateTemplatedGridView();
    
        }
    
        protected void TableGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //CreateTemplatedGridView();
            TableGridView.PageIndex = e.NewPageIndex;
            TableGridView.DataBind();
        }
    
    
        protected void btnConnect_Click(object sender, EventArgs e)
        {
            Session["IsConnectionInfoSet"] = true;
            CreateTemplatedGridView();
        }
       
        void PopulateDataTable()
        {
            Table = new DataTable();
            TableGridView.Columns.Clear();
        
            SqlDataAdapter adapter = new SqlDataAdapter("select * from customer", "Data Source=OPWFMS-7KYGZ7SB;Initial Catalog=Mayank;User ID=sa;Password=sa");
                adapter.Fill(Table);
           
        }
        void CreateTemplatedGridView()
        {
            // fill the table which is to bound to the GridView
            PopulateDataTable();
            // add templated fields to the GridView
            TemplateField BtnTmpField = new TemplateField();
            BtnTmpField.ItemTemplate =
                new DynamicallyTemplatedGridViewHandler(ListItemType.Item, "...", "Command");
            BtnTmpField.HeaderTemplate =
    
                new DynamicallyTemplatedGridViewHandler(ListItemType.EditItem, "...", "Command");
            TableGridView.Columns.Add(BtnTmpField);
    
            for (int i = 0; i < Table.Columns.Count; i++)
            {
                
                TemplateField ItemTmpField = new TemplateField();
                // create HeaderTemplate
                ItemTmpField.HeaderTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Header,
                                                              Table.Columns[i].ColumnName,
                                                              Table.Columns[i].DataType.Name);
                // create ItemTemplate
                ItemTmpField.ItemTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Item,
                                                              Table.Columns[i].ColumnName,
                                                              Table.Columns[i].DataType.Name);
                //create EditItemTemplate
                
                // then add to the GridView
                TableGridView.Columns.Add(ItemTmpField);
                
            }
    
            // bind and display the data
            TableGridView.DataSource = Table;
            TableGridView.DataBind();
        }  
    }
    and in the class file
    Code:
    public class DynamicallyTemplatedGridViewHandler : ITemplate
    {
    
        ListItemType ItemType;
        string FieldName;
        string InfoType;
    
        public DynamicallyTemplatedGridViewHandler(ListItemType item_type, string field_name, string info_type)
        {
            ItemType = item_type;
            FieldName = field_name;
            InfoType = info_type;
        }
    
        public void InstantiateIn(System.Web.UI.Control Container)
        {
            switch (ItemType)
            {
                case ListItemType.Header:
                    Literal header_ltrl = new Literal();
                    header_ltrl.Text = "<b>" + FieldName + "</b>";
                    Container.Controls.Add(header_ltrl);
                    break;
                case ListItemType.Item:
                    switch (InfoType)
                    {
                        case "Command":
                            break;
    
                        default:
                            Label field_lbl = new Label();
                            field_lbl.ID = FieldName;
                            field_lbl.Text = String.Empty; //we will bind it later through 'OnDataBinding' event
                            field_lbl.DataBinding += new EventHandler(OnDataBinding);
                            Container.Controls.Add(field_lbl);
                            break;
                    }
                    break;        
            }
        }
    
        private void OnDataBinding(object sender, EventArgs e)
        {
    
            object bound_value_obj = null;
            Control ctrl = (Control)sender;
            IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
            bound_value_obj = DataBinder.Eval(data_item_container.DataItem, FieldName);
            switch (ItemType)
            {
                case ListItemType.Item:
                    Label field_ltrl = (Label)sender;
                    field_ltrl.Text = bound_value_obj.ToString();
                    break;     
            }
    
        }
    
    }
    but for my requirements I want to use that code
    Code:
       class NumericWithNext : ITemplate
                        {
    
                        GridView localGrid;
                        int intSlotNo = 0;
    
                        #region CONTRUCTOR
    
                        public NumericWithNext(GridView gv)
                        {
                            localGrid = gv;
                            //intSlotNo = slotNo;
                            intSlotNo = localGrid.PageIndex / 10;
    
                            //constructor
                        }
          public void InstantiateIn(Control container)
                        {
                        LinkButton prevTenRecords = new LinkButton();
                        prevTenRecords.Text = "Previous 10 Pages";
                        prevTenRecords.CssClass = "PagingLnks";
                        prevTenRecords.CommandArgument = ((intSlotNo - 1) * 10 + 1).ToString(); ;
                        prevTenRecords.CommandName = "Page";
                        //prevTenRecords.Click += new EventHandler(prevTenRecords_Click);
                        prevTenRecords.Width = Unit.Pixel(125);
                        if (intSlotNo > 0)
                        {
                        container.Controls.Add(prevTenRecords);
                        }
    
                        LinkButton nextTenRecords = new LinkButton();
                        nextTenRecords.Text = "Next 10 Pages";
                        nextTenRecords.CommandName = "Page";
                        nextTenRecords.CommandArgument = ((intSlotNo + 1) * 10 + 1).ToString();
                        // nextTenRecords.Click += new EventHandler(nextTenRecords_Click);
                        nextTenRecords.CssClass = "PagingLnks";
                        nextTenRecords.Width = Unit.Pixel(118);
                        // nextTenRecords.Visible = false;
    
    
    
                        LinkButton prev = new LinkButton();
                        prev.Text = "Previous Page";
                        prev.CssClass = "PagingLnks";
                        prev.CommandArgument = "Prev";
                        prev.CommandName = "Page";
                        prev.Width = Unit.Pixel(90);
                        if (localGrid.PageIndex > 0)
                        {
                        container.Controls.Add(prev);
                        }
    
                        //for (int pagenum = 1; pagenum <= localGrid.PageCount; pagenum++)
                        for (int pagenum = (intSlotNo*10)+1; pagenum <= (intSlotNo+1)*10; pagenum++)
                        {
                        if (pagenum > localGrid.PageCount)
                        {
                        nextTenRecords.Visible = false;
                        break;
                        }
                        LinkButton pageInd = new LinkButton();
                        if (pagenum == localGrid.PageIndex + 1)
                        {
                        //pageInd.ForeColor = System.Drawing.Color.Green;PagingSelected
                        pageInd.CssClass = "PagingSelected";
                        }
                        else
                        {
                        pageInd.CssClass = "PagingLnks";
                        }
                        pageInd.ID = "PageInd_" + pagenum; 
                        pageInd.Text = pagenum.ToString();
                        pageInd.CommandName = "Page";
                        pageInd.CommandArgument = pagenum.ToString();
                        container.Controls.Add(pageInd);
                        pageInd.Width = Unit.Pixel(10);
                        }
    
                        LinkButton next = new LinkButton();
                        next.Text = " Next Page";
                        next.CommandName="Page";
                        next.CommandArgument = "Next";
                        next.CssClass = "PagingLnks";
                        next.Width = Unit.Pixel(80);
                        if (localGrid.PageIndex < localGrid.PageCount - 1)
                        {
                        container.Controls.Add(next);
                        }
    
                        container.Controls.Add(nextTenRecords);
                        }
    
                        void nextTenRecords_Click(object sender, EventArgs e)
                        {
                        //throw new Exception("The method or operation is not implemented.");
                        intSlotNo++;
    
                        }
    
                        void prevTenRecords_Click(object sender, EventArgs e)
                        {
                        //throw new Exception("The method or operation is not implemented.");
                        intSlotNo--;
                        }

    plz let me know how can i do that
    Last edited by Frinavale; Jul 13 '09, 02:24 PM. Reason: Added code tags. Please post code in [code] [/code] tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You realize that this functionality is already part of the GridView right?
    In fact it looks like were already using this functionality because you have a method that is actually handling it...

    I'm not really sure where you're having problems.
    You have a lot of code posted and it's very hard to read through the code and understand what the problem is because you haven't clearly defined what the problem is.

    You have 2 classes posted.
    Are you trying to use one class instead of the other?
    What problems are you having when you try this?

    -Frinny

    Comment

    • pupilstuff
      New Member
      • Jun 2009
      • 18

      #3
      thnks for reply

      just copy paste the code so then u come to know what I am trying to do

      actually i just want to use class NumericWithNext : ITemplate in my project ,but i am not able to do it .In short I just want to use both of the class but i a not able to do it

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I think I know what you want to do here.

        You want to combine the two classes into 1 class.

        Since you just want to display the paging template in the footer, you should be able to use the DataControlRowT ype to determine what controls to create.


        You'll have to add a parameter to the constructor that accepts the DataControlRowT ype. Then in the InstantiateIn method you'll check this to determine whether generate the data content template or the footer template.

        For example:
        Code:
        public class DynamicallyTemplatedGridViewHandler : ITemplate
        {
         
            ListItemType ItemType;
            string FieldName;
            string InfoType;
        
        
            DataControlRowType templateType;
         
            public DynamicallyTemplatedGridViewHandler(DataControlRowType template_type, ListItemType item_type, string field_name, string info_type)
            {
                ItemType = item_type;
                FieldName = field_name;
                InfoType = info_type;
        
        
        
                templateType = template_type;
            }
        
        public void InstantiateIn(System.Web.UI.Control Container)
        {
                switch (templateType )
                {
                    case DataControlRowType.Footer: 
        //Do your footer logic stuff here
                    break;
        
                   case DataControlRowType.Header:
        ///Do your header logic stuff here
                    break;
        
                    case DataControlRowType.DataRow;
        //Do your data row logic stuff here
                    break;
                  }
        }

        Comment

        • pupilstuff
          New Member
          • Jun 2009
          • 18

          #5
          hi Frinavale
          thanks for reply ,i tried to do that according to you but I am still facing problems becoz if i use that class as footer there are mismatching of public void InstantiateIn() function becoz both have different parameters

          if u can do it ,plz do it as i m very much needed of it

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by pupilstuff
            if i use that class as footer there are mismatching of public void InstantiateIn() function becoz both have different parameters
            When you change the signature of a function (the parameters it takes) then you have to modify all of the code that is calling it.

            Therefore, if you modify the constructor method for the DynamicallyTemp latedGridViewHa ndler class you'll have to change any code that calls it.

            You cannot change the signature of the InstantiateIn() method. The reason is because this method is used to implement the ITemplate interface requirements. The ITemplate interface requires that the InstantiateIn method that takes no parameters. Have you never worked with an interface before?

            You should be modifying the constructor's parameters...

            Comment

            • pupilstuff
              New Member
              • Jun 2009
              • 18

              #7
              thnks

              thnks for reply

              I never worked with interface before ,so I am having lot of problems.
              I know i have to use constructer according to parameter but how ,as coding depands on parameter ,if I will do any change in parameter code won't work

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                An interface only contains the signatures of methods, delegates or events for a class. The implementation (the code) is done in the class that Implements the interface.

                So, say you have an interface looks like:
                Code:
                interface MyInterface
                {
                    void SomeMethod();
                    int SomeOtherMethod(int a, int b);
                }
                If your class implements MyInterface it must supply code for the SomeMethod, and SomeOtherMethod method. The methods that supply the code for the methods defined in the interface must have matching signatures.

                If you take a look at the ITemplate interface you will note that it defines 1 method; the InstantiateIn() method. This method takes 1 parameter: A Control object to contain the instances of controls from the inline template.

                You can't change the number of parameters for the InstantiateIn method because your are using it to implement (provide code for) the InstantiateIn method defined in the ITemplate interface.

                Because of this you're going to have to use the constructor to pass in the data necessary to determine whether your ITemplate class is being used for displaying a header, the data, or a footer....or other.

                Please take a look at the code I posted previously....I added a private member that is used for this purpose. This member is initialized in the constructor. It is used in the InstantiateIn method to determine what controls to create.

                -Frinny

                Comment

                • pupilstuff
                  New Member
                  • Jun 2009
                  • 18

                  #9
                  i just copy paste the classes is code written by you , But I am having lot of errors

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    I'm sorry but I don't use C# very often.

                    The code I posted was meant as a guideline for you...it's probably going to have many errors in it. (especially since I start a class and don't have a closing brace "}" to end it)

                    I could post a working example of what I have but it's in VB and I think you're probably going to have a hard time understanding it.

                    Comment

                    • pupilstuff
                      New Member
                      • Jun 2009
                      • 18

                      #11
                      ya no prb ,thnks anyway for keep checking my posts
                      plz let me know if u have some other solution

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        What do you mean "let me know if u have some other solution"?
                        Are you abandoning this one?

                        I hope not because you're on the right track...

                        Comment

                        • pupilstuff
                          New Member
                          • Jun 2009
                          • 18

                          #13
                          no no,I am not abandoning this one as i am not good in interfaces so looking for other possible solution

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            Originally posted by pupilstuff
                            no no,I am not abandoning this one as i am not good in interfaces so looking for other possible solution
                            There's nothing very complicated about interfaces.

                            They are like...hmm... a blue print.

                            They outline what has to be done and then you do it. Just like a blue print for a house outlines what has to be done and the people building it use this to create a house.

                            What's neat about interfaces is it lets you create a class that other classes already know how to use.

                            Why?

                            Because the interface defines methods that are supposed to do some task. Since these method are always going to have the same result, other classes (like the GridView) can use your class, and call the defined methods to get the desired result.

                            Let's take a look at the ITemplate interface.

                            It defines the InstantiateIn method...

                            According to MSDN, the task of this method is as follows: "The InstantiateIn method defines the Control object that child controls and templates belong to."

                            In other words, when you add code for this, you specify what controls are needed to create a template used to display your data.

                            You will add the controls that you need to create template for your data, and someone else implementing the ITemplate interface will supply other controls that they need to create a template for their data.

                            The GridView control is able to use your class or the other persons class because it knows that the InstantiateIn method will define the appropriate template to display the data.

                            Does this make sense?

                            Comment

                            • pupilstuff
                              New Member
                              • Jun 2009
                              • 18

                              #15
                              ya,definately it makes sense

                              Comment

                              Working...