www.webdeveloper.com
Results 1 to 13 of 13

Thread: Datagrid paging trouble: correct view, incorrect id

  1. #1
    Join Date
    Jun 2004
    Posts
    12

    Datagrid paging trouble: correct view, incorrect id

    Hi to all,

    I've a big headache figuring out wat's happening lately on my
    paging methods...

    In a few words, I'll give an example:
    I've a datagrid which contains , let's say, 200 records.
    They're splitted up 10 per page, so what we'll have will be 20 pages..assuming:

    Pagesize = 10
    PageButtonCOunt = 10

    BUT, and here comes the pain, when i click on the three dots:
    " 1 2 3 4 5 6 7 8 9 10 ... " <- here

    The page repost itself and I view correctly

    " ... 11 12 13 14 15 16 17 18 19 20

    but if i rollover the mouse on a page number the link is, ie, for 11:
    javascript:__doPostBack('FindGrafici2$dgTimeSales$_ctl79$_ctl1','')

    sending me to page "2" !!

    Sounds like it can't maintain the state of the pager, but all seems
    "viewstated" (^_^) properly...
    What' the deal?? Anyone has a clue for this one?
    Any help would be VERY, VERY appreciated...
    tnx to all

    FaVA

  2. #2
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    You should actually have viewtstate disabled, viewstate can get very messy for large data grids, and it is not needed for pageing. I actually hate the built in pageing, and would reccomend doing the pageing by hand because it will give you more control (I also do not use data grids because I like more control, I use repeaters).

  3. #3
    Join Date
    Jun 2004
    Posts
    12
    Originally posted by PeOfEo
    You should actually have viewtstate disabled,
    That's the strange thing, actually it is all enabled.. so there is something else that is not working... sigh!

    Originally posted by PeOfEo
    viewstate can get very messy for large data grids, and it is not needed for pageing. I actually hate the built in pageing, and would reccomend doing the pageing by hand because it will give you more control (I also do not use data grids because I like more control, I use repeaters).
    Yeah, i understand. Unfortunately I have to use a Datagrid control... if i don't reach a solution soon, I'll go for a custom paging.
    Any other idea?

    Tnk you!

  4. #4
    Join Date
    Jun 2004
    Location
    Honolulu, Hawaii
    Posts
    13
    I use datagrids extensively, and have never run into this problem before. How are you binding the data and how are you handling the DataGrid1_PageIndexChanged method?
    Robert M. Teague
    Senior Programmer/Analyst
    Kamehameha Schools
    http://www.ksbe.edu

  5. #5
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    could you post your code (the script and the html/asp.net element, all of it) and I will compare it to some of mine.

  6. #6
    Join Date
    Jun 2004
    Posts
    12
    Hi guys,

    Here it is:

    DATAGRID IN PAGE:

    <asp:datagrid id="dgTimeSales" runat="server" PageSize="75" AllowPaging="True" Width="500px" OnPageIndexChanged="Page_Change"
    AllowSorting="False" AutoGenerateColumns="False" ShowFooter="True" BorderWidth="0px" CellSpacing="1">
    <AlternatingItemStyle CssClass="tblC2"></AlternatingItemStyle>
    <ItemStyle CssClass="tblC1"></ItemStyle>
    <HeaderStyle CssClass="tblHead"></HeaderStyle>
    <FooterStyle CssClass="tblHead"></FooterStyle>
    <Columns>
    <asp:TemplateColumn HeaderText="Ora">
    <HeaderStyle Font-Size="XX-Small" Font-Names="verdana"></HeaderStyle>
    <ItemStyle Wrap="False" HorizontalAlign="Right"></ItemStyle>
    <ItemTemplate>
    <asp:Label id="Label44" runat="server">
    <%# formatUptime(Container.DataItem.UpTime) %>
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    <PagerStyle Font-Size="XX-Small" Position="TopAndBottom" PageButtonCount="30" CssClass="pager"
    Mode="NumericPages"></PagerStyle>
    </asp:datagrid>



    AND THIS IS CODEBEHIND:


    The sub to Bind:

    Private Sub BindData()

    Try
    dgTimeSales.DataSource = MyDataSource
    dgTimeSales.DataBind()
    Catch ex As Exception
    Response.Write("error")
    End Try
    End Sub


    The sub to Change Page:

    Sub Page_Change(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
    dgTimeSales.CurrentPageIndex = e.NewPageIndex
    BindData()
    End Sub

    ----------------------------------------------------

    And that's it ^_^
    Tnx

  7. #7
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    Code:
    Sub Page_Load(Src As Object, E As EventArgs)
       If Not Page.IsPostBack then
          BindData()
        End If
    end sub	
    Sub BindData()
        Dim DBConn as OleDbConnection
        DBConn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & Server.MapPath("/news.mdb;"))   
    	Dim strSQL As String
    	strSQL = "Select id, posts from tblposts order by id desc"
    	Dim AdapterDockets As OleDbDataAdapter
    	Dim DataSetDockets As DataSet
    	AdapterDockets = New OleDbDataAdapter(strSQL, dbconn)
    	DataSetDockets = New DataSet()
    	AdapterDockets.Fill(DataSetDockets)
    	myDataGrid.DataSource = DataSetDockets
    	myDataGrid.DataBind()
    End Sub
    Sub myDataGrid_PageChanger(ByVal Source As Object, ByVal E As  DataGridPageChangedEventArgs)
    	myDataGrid.CurrentPageIndex = E.NewPageIndex 
    	BindData()
    end sub
    thats some pageing code of mine for a news column that happens to be access driven. Maybe that will help you. Look how I have the if not ispostback and the data binding setup.
    Last edited by PeOfEo; 07-09-2004 at 12:19 PM.

  8. #8
    Join Date
    Jun 2004
    Location
    Honolulu, Hawaii
    Posts
    13

    Arrow

    I think I see your problem. I think I have run into some similar to this in the past now that I think about it.

    Try changing your bind sub like this:

    Private Sub BindData()

    Try
    dgTimeSales.DataSource = MyDataSource
    dgTimeSales.DataBind()
    Catch ex As Exception
    If dgTimeSales.CurrentPageIndex - 1 >= 0 Then
    dgTimeSales.CurrentPageIndex = dgTimeSales.CurrentPageIndex - 1
    BindData
    End If
    End Try

    End Sub

    This way if the page index isn't valid, for whatever reason, the grid will rebind to the first page and the pager should show correctly.

    I generally find it better to not put in Try/Catch blocks too early in the development process, simply because I want the page to crash and I can track down the errors easier. Put them in later, not at the end of development though.
    Robert M. Teague
    Senior Programmer/Analyst
    Kamehameha Schools
    http://www.ksbe.edu

  9. #9
    Join Date
    Jun 2004
    Posts
    12
    Originally posted by roteague
    I think I see your problem. I think I have run into some similar to this in the past now that I think about it.

    [cut]

    Thanks roteague, your code is right, but it still doesn't solve the problem, because I don't really gat caught up in error, just the index of the number shown on pager it's wrong after the page has rendered.
    Byez

  10. #10
    Join Date
    Jun 2004
    Location
    Honolulu, Hawaii
    Posts
    13
    Are you using a DataReader or DataSet to bind to?

    http://msdn.microsoft.com/asp.net/us...lesortable.asp

    snip: with default paging you cannot use a DataReader, but instead must use a DataTable or DataSet; using a DataReader will result in an exception. The reason you cannot use a DataReader is because the DataGrid needs to be able to determine how many total records are in the DataSource so it can determine how many total pages there are.
    Robert M. Teague
    Senior Programmer/Analyst
    Kamehameha Schools
    http://www.ksbe.edu

  11. #11
    Join Date
    Jun 2004
    Posts
    12
    Originally posted by roteague
    Are you using a DataReader or DataSet to bind to?
    Currently I'm using Web Services, which returns an array of data
    that I directly use as Datasource for the Datagrid

  12. #12
    Join Date
    Jun 2004
    Location
    Honolulu, Hawaii
    Posts
    13
    Currently I'm using Web Services, which returns an array of data
    I wonder if that could be where the problem is happening. The datagrid needs to be able to step forward and backwards in order to figure out how to set up the pager. I'll have to check some of my Webservices stuff.
    Robert M. Teague
    Senior Programmer/Analyst
    Kamehameha Schools
    http://www.ksbe.edu

  13. #13
    Join Date
    Jun 2004
    Posts
    12
    Originally posted by roteague
    I wonder if that could be where the problem is happening. The datagrid needs to be able to step forward and backwards in order to figure out how to set up the pager. I'll have to check some of my Webservices stuff.
    Thank you very much roteague, I'll also keep you informed if I make progress in this situation

    Aloha! (about your place...aah, what a beautiful place )

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles