A Couple Must Read ASP.NET Articles

June 10, 2008 21:13 by Dominick

Speed up access to your favorite frameworks via the AJAX Libraries API

This is just a fantastic idea. It's very simple. Use Google's extensive infrastructure to serve those common JavaScript libraries you love to use. I've recently started using jQuery so all I have to do is point the link to the URL provided by Google and that's 17kb per request my server won't have to turn out. Thanks, Google!

CSS Message Boxes for different message types 

This comes from one of the best new sites out there "Janko At Warp Speed". This article shows us how to handle a few common message types within our applications. It's all CSS and very sharp!



Tags:
Categories: ASP.NET | Tip/Trick | General | Javascript
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed


Showing the Users Currently on your Site (Part 1)

October 18, 2007 13:59 by Dominick

Introduction

I love to see a live look at who's currently on my site. The detailed stats I get from Google Analytics is really nice, but it only gets updated every 24 hours. Sometimes I just want to see if anybody is on my site. Quick and simple. Just give me a number!

This is pretty easy to pull off when you use ASP.NET's built-in membership provider. You can show basic users online statistics on any site that uses it with just a few lines of code. In part 1 I'll show you how to get the very basic info to display. In the later parts I'll make it much more robust by showing the current authinticated and anonymous users and adding AJAX support to truely show live data without having to refresh the page. I plan on releasing the source code when it's all complete.

So you're doing what... how?

This first thing you need do it is make sure Session State is enabled. If you use BlogEngine.NET 1.2 then it's disabled by default. You need to go into the web.config file and make a change. I'll show you the section in the web.config for BlogEngine.NET, but its the same in any application.

<pages enableSessionState="true" enableViewStateMac="true" enableEventValidation="true">
   <controls>
      <add namespace="Controls" tagPrefix="blog"/>
   </controls>
</pages>

Here you just set enableSessionState to true. I'm also assuming here that you've already set up the ASP.NET Membership provider for your site. There are examples of how to do that all around the interwebs. Actually, setting up the membership provider isn't needed for part 1, but you'll need it later for the more advanced stuff.

Ah, our old friend Global.asax. It is hardly ever used anymore, but it certainly has it's uses. The counting goes on withing Global.asax. We are going to store the user count in a cache object rather than store anything in a database for performance sake. Here's what your Global.asax file should look like.

void Application_Start(object sender, EventArgs e)
{
    HttpContext.Current.Cache.Insert("UsersOnline", 0);
}
void Session_Start(object sender, EventArgs e)
{
    HttpContext.Current.Cache["UsersOnline"] = 
(int)HttpContext.Current.Cache["UsersOnline"] + 1;
}
void Session_End(object sender, EventArgs e)
{
    HttpContext.Current.Cache["UsersOnline"] = 
(int)HttpContext.Current.Cache["UsersOnline"] - 1;
}
void Application_End(object sender, EventArgs e)
{
    HttpContext.Current.Cache.Remove("UsersOnline");
}

When the application starts (compiles on the server) the cache object is created with a value of zero. When a session starts the value is increased by one. When the session ends the value is decreased by one. Finally, when the application stops the cache object is removed. All very simple and easy to insert into your Global.asax file.

All that's left is to display the cache object to the page. You can do it on any page you like within your application. All you need to use is this bit of code.

<%= HttpContext.Current.Cache["UsersOnline"].ToString() %>

That will display the current number of users on your site. How long they are considered to be on your site is determined by the session timeout period you have set.

Give me more

The next part will start getting into showing the authenticated users who are logged in. Then we'll get into the fun stuff. AJAX support. Please feel free to comment!


kick it on DotNetKicks.com



Repeater control alternating backgrounds

October 8, 2007 20:02 by Dominick
Here's an easy way to have an alternating background color with a Repeater control. There's no "AlternatingItemStyle" like in other data list controls, but this does this trick with a couple lines of code.


protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
  if (e.Item.ItemType == ListItemType.AlternatingItem)
   {
     ((HtmlTableRow)e.Item.FindControl("Row")).BgColor = "silver";
   }
}



kick it on DotNetKicks.com