Wednesday, May 21, 2008

Recent View table


While trying to set up my database, I wanted to develop a wiki to track my database changes. I chose this blog. However, I came across a website in the process. I has wiki; but more importantly, it has database capabilities. I copied an Excel table and converted it over to a database online. While working with this data, I noticed the site has a Recent View feature which keeps track of records that you have viewed. This is a nice feature that I would like to add to my application. It proved rather difficult overall, but now I have it working. Working the way I want it to as well.
First of all, I set up a web user control for this Recent View. I am using a master page and a web form to designate the content. My content consists of a GridView of the Billing Log records, Main Menu user control, and this control. I will explain this set up in later postings.
In the user control, I set up a DataList with the SqlDataSource is my RecentViewed table. I went into SQL Management Studio and created this table.
My DataList looked something like this:


<asp:DataList ID="RecentViewDataList" runat="server" DataSourceID="RecentViewedSqlDataSource" onselectedchange="RecentViewedDataList_SelectorChanged">
<HeaderTemplate>
<asp:Label ID="RecentViewedHeaderLabel" Text="Site" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="RecentViewedIDLabel" CommandArgument='<%# Eval("ID") %>' Text='<%# Eval("Site") %>' runat="server" OnClick="RecentViewClick" />
</ItemTemplate>
</asp:DataList>

Below this was my SqlDataSource:


<asp:SqlDataSource ID="RecentlyViewedSqlDataSource" runat="server" connectionstring="<%$ ConnectionStrings:DevConnectionStringCostRecovery %>" selectcommand="SELECT TOP 10 RecentViewed.RecentKeyID as 'ID', BillingLog.Site as 'Site' FROM RecentViewed INNER JOIN BillingLog ON RecentViewed.RecentKeyID = BillingLog.ID ORDER BY RecentViewed.RecentStamp DESC" />


I use SELECT TOP 10 to only show the first 10 records ordered by RecentStamp. RecentStamp is a timestamp field with a default value of GETDATE(). This automatically puts in the time when a record is inserted into the table. I set my RecentKeyID as "ID" as used in the CommandArgument above. Same for Site. The Text in the LinkButton is displayed on the webpage, but the CommandArgument is the value passed to the RecentViewClick code behind the page. This is how I can show the name of the site, but pass the record number when viewing the record.
I also added come color to the backgound item style and alternating style of the DataList, but I won't go into that here. I even put an background image similar to the background image posting.
As far as the code behind, see my next posting...

No comments: