Question

I'm trying to convert a GridView to a ListView to allow me more control over the table display.

The GridView works fine as defined here and correctly displays data.

<asp:GridView ID="gvService" runat="server"
    AllowPaging="True"
    AutoGenerateColumns="False"
    DataKeyNames="ID"
    DataSourceID="objService">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="ServiceType" HeaderText="Type" />
    </Columns>
</asp:GridView>

When I convert this to a ListView, no HTML is rendered to the page at all. I've even placed them side by side, the GridView works but the ListView doesn't display.

<asp:ListView ID="lvService" runat="server"
    DataSourceID="objService">
    <LayoutTemplate>
        <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Type</th>
        </tr>
        <tr id="itemPlaceholder" runat="server"></tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td><%# Eval("ID") %></td>
            <td><%# Eval("Name") %></td>
            <td><%# Eval("ServiceType") %></td>
        </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <tr>
            <td><%# Eval("ID") %></td>
            <td><%# Eval("Name") %></td>
            <td><%# Eval("ServiceType") %></td>
        </tr>
    </AlternatingItemTemplate>
</asp:ListView>

Here is the ObjectDataSource

<asp:ObjectDataSource ID="objService" runat="server"
    EnablePaging="true"
    TypeName="My.Data.DataSource.ServiceDataSource" DataObjectTypeName="My.Data.Service.ServiceSearch"
    SelectMethod="Search"
    SelectCountMethod="SearchCount">
    <SelectParameters>
        <asp:ControlParameter ControlID="tbSearch" Name="Search" PropertyName="Text" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

I feel like I'm missing something basic here. Any idea on why GridView populates but ListView doesn't?

Am I required to manually DataBind() the control in the code behind?

Was it helpful?

Solution

The problem here seems to be with ListView. Since you have set EnablePaging="true" for ObjectDataSource, So your ObjectDataSource will always look for a Select method having two parameters: MaximumRows & startRowIndex.

startRowIndex is incremented automatically depending on your MaximumRows setting.

In case of Grid view MaximumRows is specified using the GridView property: PageSize. In case you don't specify this, default value of 10 is used for MaximumRows .

In case of your ListView there is NO such setting as PageSize, so NO option of any default value. If you check your Select method when list view binding is to be done, you will see that the value of MaximumRows equals -1.

So, the correction you need to apply is using a DataPager in your .aspx file as below. Note that the PagedControlID property is set to ID of your List view.

<asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvService"
        PageSize="20">
        <Fields>
            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
                ShowLastPageButton="True" />
        </Fields>
    </asp:DataPager>

Again , the PageSize property specifies the MaximumRows parameter value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top