by Pieter Brinkman
23. December 2009 06:48
When using the Datapager with a ListView I had the following problem. When clicking a paging button for the first time nothing happens.But when I click a button the second time, then the page from the first click loads.
I search the internet for a solution and found that you need to add some code to the OnPagePropertiesChanging event of the list view to reload the DataPager.
The following code is the solution to my problem. Including a fix that the data doesn't get loaded two times.
[code:c#]
private List<Product> productList;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
fillGrid();
}
private void fillGrid()
{
if(productList == null)
productList = getproducts();
ListView1.DataSource = productList;
ListView1.DataBind();
DataPager1.DataBind();
}
public List<Product> getproducts()
{
using (AdventureWrksDataContext db = new AdventureWrksDataContext())
{
return db.Products.ToList();
}
}
protected void lvproducts_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
fillGrid();
}
[/code]
You can download the solution (PagingExample.zip (83.05 kb))
Cheers,
Pieter
by Pieter Brinkman
3. June 2009 07:32
By default it is not possible to use the ViewState within methods of a ObjectDataSource. Because the DataSource doesn't run within the current page instance, it just fires the method that you specified.
You can set the page instance for the DataSourceby setting the ObjectInstance property in the ObjectCreating event of the DataSource.
[code:html]
<asp:ObjectDataSource ID="odsListing" runat="server" SelectMethod="getItems" TypeName="YOUR.NAMESPACE" OnObjectCreating="ObjectDataSource_ObjectCreating" DataObjectTypeName="List<Item>">
[/code]
protected void ObjectDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = this;
}
Now you can use the ViewState in you DataSourceMethod (getItems() in this example).
by Pieter Brinkman
23. July 2008 22:57
Get dataItem onItemDataBound (don't forget to replace the bold parts with your objectname):
protected void MainPostListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
YourObject item = (YourObject)((ListViewDataItem)e.Item).DataItem;
}
Findcontrol within the itemtemplate:
HyperLink hplTitle = (HyperLink)e.Item.FindControl("hyperLink");