Wednesday, December 11, 2013

Paging with a ListView instead of a Repeater

I came across a task which required the functionality of a repeater (the ability to bind data and have it display in an HTML item template), but also the ability to page through the data (much like a GridView). There were some solutions out there, which required a lot of customization and involved too much time to make it worthwhile.

The ListView control however allows the display of bound data in an HTML template as well as other features such as: sorting, data editing, data insertions, and data deletions.

The following code is the basic structure for the ListView. The placeholder is where each item template will be placed (which is handy when you want a div or other HTML element to wrap around the data items). You can then add the DataPager, which allows for the ListView paging. You will also need the page properties changing event to ensure the paging works seamlessly.

Monday, December 2, 2013

ASP GridView HyperLink without Code Behind

The following code snippet allows for a hyperlink to be built using the GridView's data source, along with a specified URL format.

<asp:HyperLinkField HeaderText="View Newsletter" DataNavigateUrlFields="Newsletter_Edition" DataNavigateUrlFormatString="/About/Pages/Newsletter.aspx?Edition={0}" Text="View" />
It would appear in the GridViews full source, like follows:
 <asp:GridView ID="gridArchive" runat="server" AutoGenerateColumns="False" Width="800px" CssClass="newsletter">
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Issue_x0020_Date" HeaderText="Date Published" />
<asp:BoundField DataField="Edition" HeaderText="Edition" />
<asp:HyperLinkField HeaderText="View Newsletter" DataNavigateUrlFields="Edition" DataNavigateUrlFormatString="/About/Pages/Newsletter.aspx?Edition={0}" Text="View" />