www.webdeveloper.com
Results 1 to 5 of 5

Thread: DataGrid paging "previous" event failure

  1. #1
    Join Date
    Nov 2003
    Location
    Derby, UK
    Posts
    87

    DataGrid paging "previous" event failure

    Greetings :-)

    I'm having trouble getting the paging to work on one of my DataGrid web controls. The following C# code lives behind a simple ASPX page containing a single PlaceHolder control...
    PHP Code:
    private void Page_Load(object senderSystem.EventArgs e)
    {
        
    // Create a table, column and 25 rows
        
    DataTable obDataTable = new DataTable"testtable" );
        
    obDataTable.Columns.Add( new DataColumn"testcol1" ) );
        for ( 
    int iLoop=0iLoop<25iLoop++ )
            
    obDataTable.Rows.Add( new object[] { "a" iLoop.ToString() } );

        
    // Create datagrid and set the paging to 10
        
    DataGrid obDataGrid = new DataGrid();
        
    obDataGrid.AllowPaging true;
        
    obDataGrid.PageSize 10;
        
    obDataGrid.PageIndexChanged += new 
            
    DataGridPageChangedEventHandlerPageChanged );

        
    // Set the Data source, bind and add to the placeholder
        
    obDataGrid.DataSource obDataTable.DefaultView;
        
    obDataGrid.DataBind();
        
    PlaceHolder1.Controls.AddobDataGrid );
    }

    protected 
    void PageChangedobject objSender
            
    DataGridPageChangedEventArgs eArgs )
    {
        
    // Cast the datagrid and set the page index
        
    DataGrid obDataGrid = (DataGrid)objSender;
        
    obDataGrid.CurrentPageIndex eArgs.NewPageIndex;
        
    obDataGrid.DataBind();

    Moving forward through the pages works correctly, and I can breakpoint in the PageChanged handler. However the Previous button does not fire the event handler (but does fire Page_Load) and the data on screen becomes corrupt.
    Using the above code, the 3 pages show correctly, but clicking Previous on the 3rd page (which has the last 5 entries) produces a page showing the same 5 entries, plus the last 5 entries from the 1st page.
    This can be also be reproduced with the PagerStyle.Mode set to Numbered.

    Does anybody have any ideas? Why is the event handler not being called for the Previous button? Any help would be greatly appreciated.

    Cheers,
    Tom
    I am not young enough to know everything - Oscar Wilde

  2. #2
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    I do not use c# enough and have not experienced this problem before in vb.net so I will not be able help you figure out this specific problem, but just in general I would say write your own pageing code, custum pageing, instead of relying on the built in asp.net pageing because if you do your own code you will have more control over it and it will be valid, you do not have to worrie about how asp.net outputs js without a type attribute, and so on and so fourth. I am not doing anypaging right now but a blog I am working on will only show results from the past most and put the rest into the archive, which I actuall probably will end up pageing when this is all done, but when I used the built in pageing back in the day I did not like it because it gave me very limited control over the look and feel of it. Just out of curiosity, why are you using the place holder control, doesn't that output a table? Why wouldn't you use a <div runat="server">? This is just out of my curiosity, because I find myself running a bunch of html elements at the server to get more source control.

  3. #3
    Join Date
    Nov 2003
    Location
    Derby, UK
    Posts
    87
    Thanks for your reply :-)

    I have to agree with you about the DataGrids seemingly appauling control over the appearance of things like the paging.
    Also, unless I'm being stupid - which is highly possible - there doesn't seem to be a way to set a CssClass property on any of the controls within the DataGrid (in particular the <A> tag that is created for the title in sortable columns).

    But your suggestion of writing my own custom paging looks like it is going to be the answer - unfortunate, as we're looking at a pretty hefty deadline.

    The PlaceHolder control does not produce a table - it is used as more of a marker for the positioning of dynamically created ASP.NET HTML code.
    From MSDN "Use the PlaceHolder control as a container to store dynamically added server controls to the Web page. The PlaceHolder control does not produce any visible output and is only used as a container for other controls on the Web page."
    Do a quick test - make a simple "<html><body><asp:PlaceHolder ID="phTest"/></body></html>" page, and add a single Label object to the controls collection. The result is just a <span> tag (and associated attributes), nothing else.
    I am not young enough to know everything - Oscar Wilde

  4. #4
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    well does it have to infact be pageing? I wqould say count the rows from the gird like before, then divide it by 10. Then show the first 10 results, then show the next 10 when the user clicks on the next button (multiply the page# (which will incriment when the user clicks it) by 10 then add another 10 and you have your beginning and end numbers, page# * 10 = your start, page# * 10 + 10 = your end), so on and so fourth until he is on the last page where you would make the button disabled or no longer visible. A button would be good because it just resends the data, but you could also use a query string and redirect that user to the same page he was on but with the query string for the page number and it will run that page load select event and get the number of entries again too, which would look better but be slightly less efficient (but probably not noticable). You could also use the linkbutton element, to avoid this, but that element will create a java script even though it will resend the page. I try to avoid asp.net outputting js at all costs.
    Last edited by PeOfEo; 04-23-2004 at 04:10 PM.

  5. #5
    Join Date
    Aug 2005
    Posts
    1

    Have this issue been solved?

    I met the same problem. Just wonder if it has been solved.

    Thanks!

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