Tuesday, July 15, 2008

Data paging List View in .Net 3.5

ListView is one of the new data controls that was shipped with the latest release of ASP.NET, which is 3.5. It displays the values from a data source by utilizing user-defined templates. This gives the developer more flexibility about the design of the data displayed on the user interface. In order for the ListView control to display its content, templates should be created for different parts of the control. The LayoutTemplate and ItemTemplate are mandatory. All other templates are optional.

Here is a sample to use Data paging without Data Source (Data Bind)

<asp:ListView ID="ListView1" runat="server" 
onpagepropertieschanging="ListView1_PagePropertiesChanging">
<LayoutTemplate>
LIST VIEW <br />
<table>
<tr>
<th>Court ID</th>
</tr>
<tr id="itemPlaceHolder" runat="server" />
</table>

</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
test<%
   1: # Eval("CourtID")
%>
</td>
</tr>

</ItemTemplate>
</asp:ListView>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="3" >
<Fields>
<asp:NumericPagerField ButtonCount="4" />
</Fields>
</asp:DataPager>




//inside page load
protected void Page_Load(object sender, EventArgs e)
{
//nomd for the first time
if (!IsPostBack)
{
ListView1.DataSource = CourtProvider.GetAllCourtsForDataBinding();
ListView1.DataBind();
}
}




//when ListView Page Property Changed
protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

ListView1.DataSource = CourtProvider.GetAllCourtsForDataBinding();
ListView1.DataBind();

}







Here is simple way to display using Object Data Source




<asp:ListView ID="ListView1" runat="server" DataSourceID="pdsData">
<LayoutTemplate>
LIST VIEW <br />
<table>
<tr>
<th>Court ID</th>
</tr>
<tr id="itemPlaceHolder" runat="server" />
</table>

</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
test<%
   1: # Eval("CourtID")
%>
</td>
</tr>

</ItemTemplate>
</asp:ListView>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="3" >
<Fields>
<asp:NumericPagerField ButtonCount="4" />
</Fields>
</asp:DataPager>

<cc:ParentDataSource ID="pdsData" runat="server" SelectMethod="GetData" SelectCountMethod="GetDataRowCount" />




public IEnumerable<CourtBindingHelper> GetData(string sortExpression, int maximumRows, int startRowIndex)
{
try
{

return this.CourtsData.GetSortPagingData(sortExpression, maximumRows, startRowIndex);

}
catch (Exception ex)
{
lblError.Text = ex.Message;
//if error just return the agencies
return this.CourtsData;
}
}

public int GetDataRowCount()
{
return (this.CourtsData != null ? this.CourtsData.Count : 0);
}

No comments: