tags:

views:

1035

answers:

3

Hi ,

I have a greidview nested within a repeater , And trying to enable paging on th e grid view without much success.

the gridview databound is like this

<asp:repeater....> 
    <asp:gridview id="GridView1" Datasource='<%# LoadData(CInt(Eval("Id"))) %>' 
         OnPageIndexChanging="GridViewPageIndexChanging" AllowPaging="true" 
PageSize="10" ............. </asp:GridView>
</asp:repeater>

In the code behind My loadData get a list of objects.

Public Function LoadData(ByVal Id As Integer) As IList(Of Client)
            Dim ds As IList(Of Client) = client.GetClientById(Id)
            Return ds
        End Function

And the event handler is as follow:

Protected Sub GridViewPageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
            sender.PageIndex = e.NewPageIndex
End Sub

My code doesn't change the page in the gridview , Am I missing something ?

Any help is very appreciate it.

+2  A: 

You have to call DataBind().

Dim grid as GridView = DirectCast(sender, GridView)
grid.PageIndex = e.NewPageIndex
grid.DataBind()

EDIT

Since I can't comment yet and creating another answer to an answer makes everything confusing, I'll just edit this one.

I'm afraid the Repeater is a wrong control to use for what you want. The problem stems from the fact that it does not preserve DataItem when GridView's page events fire. So the "id" gets evaluated into nothing and subsequently a zero. Btw, in C# you'd get a null exception.

I suggest you use the DataList instead:

<asp:DataList ID="DataList" runat="server" DataKeyField="id">
    <ItemTemplate>
     <asp:GridView ID="Grid" runat="server"
      AllowPaging="true"
      PageSize="2"
      OnPageIndexChanging="Grid_PageIndexChanging"
      DataSource='<%# GetData(DirectCast(DataList.DataKeys(DirectCast(Container, DataListItem).ItemIndex), Integer)) %>'
     >
     </asp:GridView>
     <hr />
    </ItemTemplate>
</asp:DataList>

With code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
     DataList.DataSource = New Integer() {1, 2, 3, 4, 5}.Select(Function(x) New With {.id = x})
     DataList.DataBind()
    End If
End Sub

Protected Function GetData(ByVal id As Integer) As String()
    Dim arr As String() = New String(4) {}
    For i As Integer = 0 To arr.Length - 1
     arr(i) = String.Format("id {0}; grid item {1}", id, i)
    Next
    Return arr
End Function

Protected Sub Grid_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
    Dim grid As GridView = DirectCast(sender, GridView)
    grid.PageIndex = e.NewPageIndex
    grid.DataBind()
End Sub

This code works -- I tested. Although, personally I don't like using binding expressions.

Ruslan
Thanks ruslan whene I do that I got the following error:"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
Youssef
A: 

One thing I see if your markup. Your sample has the GridView directly within the Repeater control. You will need it within the ItemTemplate.

<asp:Repeater ID="rpt" runat="server" ...>
   <ItemTemplate>
      <asp:GridView id="gv1" runat="server" ...>
      .
      .
      .
      </asp:GridView>
   </ItemTemplate>
</asp:Repeater>
Dan Appleyard
It is inside an ItemTemplate,
Youssef
A: 

I'm one step further , After I do the binding I got an exception on Eval. I changed it in the datasource to <%#LoadData(DataBinder.Eval(Container.DataItem,"Id"))%>

I don't get the Eval exception anymore after the binding. However my grid still empty.

[update]

Ok I got it solved by keeping my Ids in hashtable and I rebind my gridview with the equivalent id from the hashtable.

It is all good now thanks everyone for your helps.

Youssef