468,247 Members | 1,460 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

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
Expand|Select|Wrap|Line Numbers
  1.   <asp:GridView ID="TableGridView"  
  2.  
  3.       OnPageIndexChanging="TableGridView_PageIndexChanging"      
  4.       runat="server"  AutoGenerateColumns="False" 
  5.      AllowPaging="True" AllowSorting="True" >
  6.  
  7.  </asp:GridView>
  8.  
  9.         <asp:Button ID="btnConnect" runat="server"  Style="z-index: 113;
  10.             left: 260px; position: absolute; top: 143px" Text="Connect & Populate" OnClick="btnConnect_Click" />
in code behind page
Expand|Select|Wrap|Line Numbers
  1. public partial class _Default : System.Web.UI.Page
  2. {
  3.  
  4.     public static DataTable Table = new DataTable();
  5.     ArrayList ParameterArray = new ArrayList();
  6.  
  7.  
  8.     protected void Page_Load(object sender, EventArgs e)
  9.     {
  10.         if (IsPostBack && (bool)Session["IsConnectionInfoSet"]==true)
  11.             CreateTemplatedGridView();
  12.  
  13.     }
  14.  
  15.     protected void TableGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  16.     {
  17.         //CreateTemplatedGridView();
  18.         TableGridView.PageIndex = e.NewPageIndex;
  19.         TableGridView.DataBind();
  20.     }
  21.  
  22.  
  23.     protected void btnConnect_Click(object sender, EventArgs e)
  24.     {
  25.         Session["IsConnectionInfoSet"] = true;
  26.         CreateTemplatedGridView();
  27.     }
  28.  
  29.     void PopulateDataTable()
  30.     {
  31.         Table = new DataTable();
  32.         TableGridView.Columns.Clear();
  33.  
  34.         SqlDataAdapter adapter = new SqlDataAdapter("select * from customer", "Data Source=OPWFMS-7KYGZ7SB;Initial Catalog=Mayank;User ID=sa;Password=sa");
  35.             adapter.Fill(Table);
  36.  
  37.     }
  38.     void CreateTemplatedGridView()
  39.     {
  40.         // fill the table which is to bound to the GridView
  41.         PopulateDataTable();
  42.         // add templated fields to the GridView
  43.         TemplateField BtnTmpField = new TemplateField();
  44.         BtnTmpField.ItemTemplate =
  45.             new DynamicallyTemplatedGridViewHandler(ListItemType.Item, "...", "Command");
  46.         BtnTmpField.HeaderTemplate =
  47.  
  48.             new DynamicallyTemplatedGridViewHandler(ListItemType.EditItem, "...", "Command");
  49.         TableGridView.Columns.Add(BtnTmpField);
  50.  
  51.         for (int i = 0; i < Table.Columns.Count; i++)
  52.         {
  53.  
  54.             TemplateField ItemTmpField = new TemplateField();
  55.             // create HeaderTemplate
  56.             ItemTmpField.HeaderTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Header,
  57.                                                           Table.Columns[i].ColumnName,
  58.                                                           Table.Columns[i].DataType.Name);
  59.             // create ItemTemplate
  60.             ItemTmpField.ItemTemplate = new DynamicallyTemplatedGridViewHandler(ListItemType.Item,
  61.                                                           Table.Columns[i].ColumnName,
  62.                                                           Table.Columns[i].DataType.Name);
  63.             //create EditItemTemplate
  64.  
  65.             // then add to the GridView
  66.             TableGridView.Columns.Add(ItemTmpField);
  67.  
  68.         }
  69.  
  70.         // bind and display the data
  71.         TableGridView.DataSource = Table;
  72.         TableGridView.DataBind();
  73.     }  
  74. }
and in the class file
Expand|Select|Wrap|Line Numbers
  1. public class DynamicallyTemplatedGridViewHandler : ITemplate
  2. {
  3.  
  4.     ListItemType ItemType;
  5.     string FieldName;
  6.     string InfoType;
  7.  
  8.     public DynamicallyTemplatedGridViewHandler(ListItemType item_type, string field_name, string info_type)
  9.     {
  10.         ItemType = item_type;
  11.         FieldName = field_name;
  12.         InfoType = info_type;
  13.     }
  14.  
  15.     public void InstantiateIn(System.Web.UI.Control Container)
  16.     {
  17.         switch (ItemType)
  18.         {
  19.             case ListItemType.Header:
  20.                 Literal header_ltrl = new Literal();
  21.                 header_ltrl.Text = "<b>" + FieldName + "</b>";
  22.                 Container.Controls.Add(header_ltrl);
  23.                 break;
  24.             case ListItemType.Item:
  25.                 switch (InfoType)
  26.                 {
  27.                     case "Command":
  28.                         break;
  29.  
  30.                     default:
  31.                         Label field_lbl = new Label();
  32.                         field_lbl.ID = FieldName;
  33.                         field_lbl.Text = String.Empty; //we will bind it later through 'OnDataBinding' event
  34.                         field_lbl.DataBinding += new EventHandler(OnDataBinding);
  35.                         Container.Controls.Add(field_lbl);
  36.                         break;
  37.                 }
  38.                 break;        
  39.         }
  40.     }
  41.  
  42.     private void OnDataBinding(object sender, EventArgs e)
  43.     {
  44.  
  45.         object bound_value_obj = null;
  46.         Control ctrl = (Control)sender;
  47.         IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
  48.         bound_value_obj = DataBinder.Eval(data_item_container.DataItem, FieldName);
  49.         switch (ItemType)
  50.         {
  51.             case ListItemType.Item:
  52.                 Label field_ltrl = (Label)sender;
  53.                 field_ltrl.Text = bound_value_obj.ToString();
  54.                 break;     
  55.         }
  56.  
  57.     }
  58.  
  59. }
but for my requirements I want to use that code
Expand|Select|Wrap|Line Numbers
  1.    class NumericWithNext : ITemplate
  2.                     {
  3.  
  4.                     GridView localGrid;
  5.                     int intSlotNo = 0;
  6.  
  7.                     #region CONTRUCTOR
  8.  
  9.                     public NumericWithNext(GridView gv)
  10.                     {
  11.                         localGrid = gv;
  12.                         //intSlotNo = slotNo;
  13.                         intSlotNo = localGrid.PageIndex / 10;
  14.  
  15.                         //constructor
  16.                     }
  17.       public void InstantiateIn(Control container)
  18.                     {
  19.                     LinkButton prevTenRecords = new LinkButton();
  20.                     prevTenRecords.Text = "Previous 10 Pages";
  21.                     prevTenRecords.CssClass = "PagingLnks";
  22.                     prevTenRecords.CommandArgument = ((intSlotNo - 1) * 10 + 1).ToString(); ;
  23.                     prevTenRecords.CommandName = "Page";
  24.                     //prevTenRecords.Click += new EventHandler(prevTenRecords_Click);
  25.                     prevTenRecords.Width = Unit.Pixel(125);
  26.                     if (intSlotNo > 0)
  27.                     {
  28.                     container.Controls.Add(prevTenRecords);
  29.                     }
  30.  
  31.                     LinkButton nextTenRecords = new LinkButton();
  32.                     nextTenRecords.Text = "Next 10 Pages";
  33.                     nextTenRecords.CommandName = "Page";
  34.                     nextTenRecords.CommandArgument = ((intSlotNo + 1) * 10 + 1).ToString();
  35.                     // nextTenRecords.Click += new EventHandler(nextTenRecords_Click);
  36.                     nextTenRecords.CssClass = "PagingLnks";
  37.                     nextTenRecords.Width = Unit.Pixel(118);
  38.                     // nextTenRecords.Visible = false;
  39.  
  40.  
  41.  
  42.                     LinkButton prev = new LinkButton();
  43.                     prev.Text = "Previous Page";
  44.                     prev.CssClass = "PagingLnks";
  45.                     prev.CommandArgument = "Prev";
  46.                     prev.CommandName = "Page";
  47.                     prev.Width = Unit.Pixel(90);
  48.                     if (localGrid.PageIndex > 0)
  49.                     {
  50.                     container.Controls.Add(prev);
  51.                     }
  52.  
  53.                     //for (int pagenum = 1; pagenum <= localGrid.PageCount; pagenum++)
  54.                     for (int pagenum = (intSlotNo*10)+1; pagenum <= (intSlotNo+1)*10; pagenum++)
  55.                     {
  56.                     if (pagenum > localGrid.PageCount)
  57.                     {
  58.                     nextTenRecords.Visible = false;
  59.                     break;
  60.                     }
  61.                     LinkButton pageInd = new LinkButton();
  62.                     if (pagenum == localGrid.PageIndex + 1)
  63.                     {
  64.                     //pageInd.ForeColor = System.Drawing.Color.Green;PagingSelected
  65.                     pageInd.CssClass = "PagingSelected";
  66.                     }
  67.                     else
  68.                     {
  69.                     pageInd.CssClass = "PagingLnks";
  70.                     }
  71.                     pageInd.ID = "PageInd_" + pagenum; 
  72.                     pageInd.Text = pagenum.ToString();
  73.                     pageInd.CommandName = "Page";
  74.                     pageInd.CommandArgument = pagenum.ToString();
  75.                     container.Controls.Add(pageInd);
  76.                     pageInd.Width = Unit.Pixel(10);
  77.                     }
  78.  
  79.                     LinkButton next = new LinkButton();
  80.                     next.Text = " Next Page";
  81.                     next.CommandName="Page";
  82.                     next.CommandArgument = "Next";
  83.                     next.CssClass = "PagingLnks";
  84.                     next.Width = Unit.Pixel(80);
  85.                     if (localGrid.PageIndex < localGrid.PageCount - 1)
  86.                     {
  87.                     container.Controls.Add(next);
  88.                     }
  89.  
  90.                     container.Controls.Add(nextTenRecords);
  91.                     }
  92.  
  93.                     void nextTenRecords_Click(object sender, EventArgs e)
  94.                     {
  95.                     //throw new Exception("The method or operation is not implemented.");
  96.                     intSlotNo++;
  97.  
  98.                     }
  99.  
  100.                     void prevTenRecords_Click(object sender, EventArgs e)
  101.                     {
  102.                     //throw new Exception("The method or operation is not implemented.");
  103.                     intSlotNo--;
  104.                     }

plz let me know how can i do that
Jul 10 '09 #1
16 3542
Frinavale
9,735 Expert Mod 8TB
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
Jul 13 '09 #2
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
Jul 13 '09 #3
Frinavale
9,735 Expert Mod 8TB
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 DataControlRowType to determine what controls to create.


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

For example:
Expand|Select|Wrap|Line Numbers
  1. public class DynamicallyTemplatedGridViewHandler : ITemplate
  2. {
  3.  
  4.     ListItemType ItemType;
  5.     string FieldName;
  6.     string InfoType;
  7.  
  8.  
  9.     DataControlRowType templateType;
  10.  
  11.     public DynamicallyTemplatedGridViewHandler(DataControlRowType template_type, ListItemType item_type, string field_name, string info_type)
  12.     {
  13.         ItemType = item_type;
  14.         FieldName = field_name;
  15.         InfoType = info_type;
  16.  
  17.  
  18.  
  19.         templateType = template_type;
  20.     }
  21.  
  22. public void InstantiateIn(System.Web.UI.Control Container)
  23. {
  24.         switch (templateType )
  25.         {
  26.             case DataControlRowType.Footer: 
  27. //Do your footer logic stuff here
  28.             break;
  29.  
  30.            case DataControlRowType.Header:
  31. ///Do your header logic stuff here
  32.             break;
  33.  
  34.             case DataControlRowType.DataRow;
  35. //Do your data row logic stuff here
  36.             break;
  37.           }
  38. }
Jul 13 '09 #4
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
Jul 15 '09 #5
Frinavale
9,735 Expert Mod 8TB
@pupilstuff
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 DynamicallyTemplatedGridViewHandler 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...
Jul 15 '09 #6
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
Jul 15 '09 #7
Frinavale
9,735 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. interface MyInterface
  2. {
  3.     void SomeMethod();
  4.     int SomeOtherMethod(int a, int b);
  5. }
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
Jul 15 '09 #8
i just copy paste the classes is code written by you , But I am having lot of errors
Jul 15 '09 #9
Frinavale
9,735 Expert Mod 8TB
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.
Jul 15 '09 #10
ya no prb ,thnks anyway for keep checking my posts
plz let me know if u have some other solution
Jul 15 '09 #11
Frinavale
9,735 Expert Mod 8TB
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...
Jul 15 '09 #12
no no,I am not abandoning this one as i am not good in interfaces so looking for other possible solution
Jul 15 '09 #13
Frinavale
9,735 Expert Mod 8TB
@pupilstuff
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?
Jul 15 '09 #14
ya,definately it makes sense
Jul 15 '09 #15
Frinavale
9,735 Expert Mod 8TB
Good :)

Let's take a look at what you have right now....
Jul 15 '09 #16
Frinavale
9,735 Expert Mod 8TB
Right now you're using a ListItemType to define what your Template should look like; but, there's something better suited for this... the DataControlRowType.

The reason it's better suited is because it specifies the function of a row in a data control, such as a DetailsView or GridView control.

The ListItemType specifies the type of an item in a list control.

So the first change you should make to your DynamicallyTemplatedGridViewHandler class is to use the DataControlRowType instead of the ListItemType.

Why don't we start with something simple and work our way up?
I don't have your database, don't know what your data looks like or how it you want it to be formatted. So I think working with a simpler example project might be easier for both of us.
Jul 15 '09 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Clint | last post: by
reply views Thread by Raed Sawalha | last post: by
2 posts views Thread by asad | last post: by
reply views Thread by Sjaakie | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.