views:

166

answers:

5

Hi, I have a DataGrid in a USerControl. Somehow the paging doesnt work, the paging has the right amount of pages, but clicking the numbers does not work ... it stays on page 1. This is my Grid:

<asp:DataGrid ID="DG_Grid" runat="server" AllowPaging="True" PageSize="10" EnableViewState="True"
            AllowSorting="False" DataKeyField="DUEDATE" OnItemDataBound="DG_Grid_ItemDataBound" OnItemCommand="DG_Grid_ItemCommand">

Ideas anyone?

A: 

Have you handled PageIndexChanged event? See here for more details.

sashaeve
Yes, I took care of it...
grady
A: 

It seems that it is not even firing the event ... i added this to my Grid:

OnPageIndexChanged="DG_Installments_PageIndexChanged"

and in my cide im listening:

protected void DG_Installments_PageIndexChanged(object sender, System.EventArgs e)
{
        //do something
}

Its never going to "do something"...

grady
Its called DG_Installments, thats ok
grady
A: 

Is the Usercontrols' Datagrid binded in the Page or in the Usercontrol itself? I think you bind them in the page and you dou sort them in the page. So you have to raise the PageIndexChanged Event from the UserControl and handle it in the page.

This exmaple is with GridView but for DataGrid its the same.

In the UserControl define an event that you handle in your Page:

    Public Event GridPageChanged(ByVal grid As GridView)

    Private Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PageIndexChanged
        RaiseEvent GridPageChanged(Me.GridView1)
    End Sub

You can now catch the usercontrol's GridPageChanged in your Page and do the sorting.

Tim Schmelter
Its bound in the UserControl itself.
grady
You only do a DataBind on the first time(!Page.IsPostBack)?!Because when you databind the grid the pageindexchanged event will not fire.
Tim Schmelter
Yeah, I only bind the datasource if(!ispostback). On the other hand, binding it each time wont help either.
grady
Was your idea to bind it each time? Not just if(!ispostback)?
grady
A: 

We need to see the rest of your code. Are you setting the page index and then rebinding? Obviously not, if you can't even get the PageIndexChanged event to fire.

Bryan
A: 

Hello, here is my code:

<asp:DataGrid ID="DG_Installments" runat="server" AllowPaging="true" ShowPager="true" 
            PageSize="10" AllowSorting="False" DataKeyField="DUEDATE" 
            EnableViewState="true"  
            OnItemDataBound="DG_Installments_ItemDataBound" 
            OnItemCommand="DG_Installments_ItemCommand"
            OnPageIndexChanged="DG_Installments_PageIndexChanged">
            ...
            some template columns
            ...

As mentioned, this is in my ascx file. Now I set a breakpoint to stop at the DG_Installments_PageIndexChanged method, which looks like that:

protected void DG_Installments_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
    {
        //do Something
    }

Thing is, its not stopping there when I click one of the pagers. I am binding the datasource in the Page_Load of the ascx. This looks like that:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Bind();
    }
}

And finally the Bind() method looks like that:

private void Bind()
{
    DG_Installments.DataSource = Installments;
    DG_Installments.DataBind();
}

The Installments property is a generic list which I build on my own. The pagers are shown, but the grids counter at the bottom always show no hits... it looks like that: 1--1 of -1 matches (not sure if thats the correct translation, I only have a german version :)).

This ascx is placed in a normal aspx.

Anyone can help?

grady