I wanted to set the QueryStringField property of the DataPager dynamically. By default, if you do not specify this property, the pager works on a Postback Model.
On the other hand, if you do set the QueryStringField to something, it'll use the value that you set the QueryStringField to as a query string parameter and assign it the appropriate page number (IOW, it makes use of a query string parameter to change the page view)
The declarative and code-behind files are simple enough..
Image may be NSFW.
Clik here to view.
public partial class _Default : System.Web.UI.Page { public string FieldName { get { return "PageID"; } } protected override void OnInit(EventArgs e) { base.OnInit(e); ListView1.PagePropertiesChanging += new EventHandler(ListView1_PagePropertiesChanging); DataPager1.QueryStringField = FieldName; } void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) { DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); BuildAndBind(); } protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { BuildAndBind(); } } private void BuildAndBind() { ListView1.DataSource = BuildDataSource(); ListView1.DataBind(); } private List BuildDataSource() { Person a = new Person { FirstName = "A", LastName = "A", Address = "Address1" }; Person b = new Person { FirstName = "B", LastName = "B", Address = "Address2" }; Person c = new Person { FirstName = "C", LastName = "C", Address = "Address3" }; Person d = new Person { FirstName = "D", LastName = "D", Address = "Address4" }; Person e = new Person { FirstName = "E", LastName = "E", Address = "Address5" }; Person f = new Person { FirstName = "F", LastName = "F", Address = "Address6" }; Person g = new Person { FirstName = "G", LastName = "G", Address = "Address7" }; Person h = new Person { FirstName = "H", LastName = "H", Address = "Address8" }; Person i = new Person { FirstName = "I", LastName = "I", Address = "Address9" }; Person j = new Person { FirstName = "J", LastName = "J", Address = "Address10" }; Person k = new Person { FirstName = "K", LastName = "K", Address = "Address11" }; Person l = new Person { FirstName = "L", LastName = "L", Address = "Address12" }; Person m = new Person { FirstName = "M", LastName = "M", Address = "Address13" }; List list = new List(); list.Add(a); list.Add(b); list.Add(c); list.Add(d); list.Add(e); list.Add(f); list.Add(g); list.Add(h); list.Add(i); list.Add(j); list.Add(k); list.Add(l); list.Add(m); return list; } }
So far...So good..
Using the above example, since we've set the property in the code behind, we'd expect the pager to use a query string to drive the page view. Commenting the line out forces the pager to use a postback model.
What about .Net 4?
the above situations work when the project is set up to run in .NET 3.5 . However, this solution does not seem to work with .NET 4.0 at all. If we set the Target Framework to 4.0, the paging does not work if we want to force a QueryStringField at runtime :(
I've attached a copy of the vs2010 website here
and the video (in mp4 format...) here
If there's anyone out there who can give me a clue on what's right or wrong, please drop me a few lines.. :)