Tuesday, August 5, 2008

Embed LinkButtons inside Repeater controls

Introduction

So you'd like to use a Repeater control for their ease of HTML formatting over an DataGrid or perhaps you're just nostalgic of coding looping table rows from a database, but you need to have the functionality of the "Select Command" button that the DataGrid control offers.

Phear not, you can create any number of columns with functional LinkButtons that can be tied to protected methods on the code behind page. Here's how...

The HTML Part...

First, within your repeater, choose where you're link column will be. In this example it's the first column. Note the LinkButton tags. Simply replace the data elements shown here. OnCommand will be the name of the c# function that will handle the click. CommandArgument will contain the unique identifier for your data, or whatever data element you want to pass back to your function to aid in processing. Finally, the text between LinkButton tags will contain whatever text, or data element you want displayed as the link, in this case the data field 'Name'. Additionally, we're giving the link a unique name by appending the 'SerialNo' column to 'EditLink' creating values like EditLink1, EditLink5, etc.


<table>
<tr>
<th>Name</th>
<th>Rank</th>
</tr>
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<tr style="background-color:whitesmoke;">
<td>
<asp:LinkButton ID="lnkEditItem" Runat="server" OnCommand="EditItemClick"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "serialno") %>'
CommandName='EditLink<%# DataBinder.Eval(Container.DataItem, "serialno") %>'
><%# DataBinder.Eval(Container.DataItem, "name") %></asp:LinkButton>
</td>
<td><%# DataBinder.Eval(Container.DataItem,"rank") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>



The C# part...

So you have a unique set of link buttons which are calling a c# method, and passing the 'SerialNo' field value. Now we need to create the c# function that will handle the click, read the passed value and do something with it. So in the code behind, you'll need something like this.

Note the method must be of type protected and have a signiture matching the one below in order for it to receive the click from the link button.


protected void EditItemClick(Object sender, CommandEventArgs e)
{
// Get the SerialNo value passed
string thisid = e.CommandArgument.ToString();

// Do something with the value
DisplayRecord(thisid);

return;
}



Conclusion

So in conclusion here's a simple way to add dynamic button functionality to a repeater control that can ultimately create a table with clickable link for each record.

Please note that you can use this method to add as many link columns as you want to your table. For example if you want a 'Delete' button, create another column within your repeating section containing a LinkButton. You must assign it to a new C# method, give the links unique CommandNames, but do the same passing the value you need for the operation you want to perform, create the function and you're set.

Friday, August 1, 2008

Google Analytics

If you're like most web developers, you collect logs of all your web traffic. Perhaps as the raw server logs, perhaps you log to Sql Server, and perhaps you've even done some reporting on those logs to analyze or categorize web traffic. I know I've been doing this for a number of years, coding simple ASP or .Net tools for navigating and aggrigating through the plethora of data.

Now Google Analytics has been around for quite some time now, and there's a ton of people who've been using it a long time. But as one who's always preferred to develop my own solutions, I've always had the opinion, I can do it better. But recently I had the itch to go see what GA is all about.

After setting up a small site with Analytics, using it for a few weeks, and exploring it's rich user interface, data drilling capapbilities, report export capabilities, report sharing, and so on I was simply amazed.

Analytics Accounts

The first thing I liked about Analytics was it's security model. You have three layers: Users, Analytics Accounts, and Web Sites. You can create any number of Analytics accounts to include groupings of websites. That is, say you design websites for three companies; two of which have a single website, and one has three websites. You can create an Analytics account for each company, adding however many sites each company has to their Analytics accounts, then grant any number of people access to each of those accounts, and you retain the ability to administer all the sites from your single login.

Setup

Once you create a Analytics account, and add a website to it, Google makes it very simple, to cut and paste a small piece of Javascript into your website. Ideally if your site(s) have programatic page templates (aka Masterpages in c#), you can simply add to Javascript to your template to streamline it's implementation into all your pages.

Once implemented, Google take about 24 hours to reaggrigate your results so you won't see any data until the day following implementation, and obviously several weeks to months before you have a good set of meaningful data to analyze.

Security

When you grant a new user access to an Analytics account, they can be granted one of two roles:


  • Administrators can modify that Analytics account adding and modifying other users access to this collection of sites.

  • Reports Only users can only view reports for the sites includes in this Analytics account.



A new account can be any existing e-mail address, or you can take it a step further and create a GMail account specifically for users of a particular Analytics account to view thier collection of websites.

For example, create a GMail account of CompanyA@gmail.com, then grant Reports Only access to that new account. You can then give out this username and password to anyong needing to view data for this account.