in

The Web Jedi

Home of Halifax's Web Jedi

Jedi Meditations

June 2007 - Posts

  • The Easy Way To Alternate Background Colours on a Repeater

    The ASP.NET Repeater is one of my favourite controls because it provides total flexibility in the layout of your data. I'll often use it to generate a list of data similar to a DataGrid or DataList. However, if I want to alternate the background colour of each row of data, the usual prescribed method is to use the AlternatingItemTemplate. But this is a such a waste of code and can be a pain to maintain if your ItemTemplate has any kind of complexity.

    There is a much easier method, though. A shortcut if you will. If you're using your Repeater to generate table rows, you can use the following code in your <tr> tag to change the CSS class for each alternating row:

    <tr class="<%# IIf(Container.ItemIndex Mod 2 = 0, "rowOdd", "rowEven") %>">

    Or, in C#:

    <tr class="<%# Container.ItemIndex % 2 == 0 ? "rowOdd" : "rowEven" %>">

    Easy peasy. This isn't really all that ingenious but every time I want to use this technique I forget the syntax (it's the "Container.ItemIndex" I can never remember) so I decided to write a blog entry so I'll have it for reference.  :-)

  • A Cool ASP.NET 2.0 Feature (Well, Almost)

    Whenever I start a new web project, one of the first things I do is create a "Base" class for all my ASPX code-beside ("code-behind" is so .NET 1.1) class declarations to inherit from. This makes it easy to have useful properties such as CurrentUser available in all pages automatically. Such a practice is quite common these days, and has been covered in several articles including this one.

    Recently, I have found it useful to have some boolean properties on the base class which control the rendering of certain elements. For example, a current project I'm working on has the requirement to restrict the user's ability to navigate backwards by using the browser's "Back" button. This is accomplished via the use of the JavaScript "history.forward()" hack. Now, some pages in the application require this while others do not. So the best way to handle this is to have a base-class property called DisableBackButton and set it to "true" when I want to prevent the user from going back to the previous page. And, in fact, this is what I did and it works well.

    However, setting such properties can only be achieved with programmatic code. For example, to turn this property "on" for a certain page, I would have to write "DisableBackButton = true;" in the Page_Load event handler. This is fine, but it feels "dirty" and unsophisticated to me. I would much prefer to set this property in a "declarative" fashion on the actual ASPX file rather than writing code to set it. Say, wouldn't it be cool if I could just add an attribute to the @Page directive that said DisableBackButton="True"? Well, it turns out that in ASP.NET 2.0, you can do exactly this!

    Well, almost. If you read all the comments in that link you'll see that it isn't all that simple, unfortunately. First, if you're using a base class like I am, you also have to set the "CodeFileBaseClass" attribute in the @Page directive. Ick. I think this is something that the compiler can determine for itself without me having to tell it explicitly. Requiring me to add it manually just means there's another possible point-of-failure and yet something else that has to be maintained. Still, it's not a too terrible price to pay for the cool declarative property setting feature I'm trying to achieve. But, that isn't the only problem. While the project will now compile, you'll still get ASP.NET validation errors in all your pages saying that "DisableBackButton" is a not a valid attribute for the @Page directive. Grrrr. To stop this message from clogging your Visual Studio errors window, you have to open up Visual Studio's XML-based schema file and add the attribute. Now this is really a problem since this schema file is used for ALL Visual Studio web projects, not just the one you're working on right now. And it also means that if you share the code with someone else, you have to tell them to go modify their schema file, too. This is completely unacceptable.

    In the end I decided to go the old-fashioned route and set this property programmatically in each page that needed it. While the declarative route would have been uber-cool, there are just too many road-bumps down that path. Here's hoping Orcas (or Shamu as my wife calls it) does something to address this problem.

    Posted Jun 17 2007, 10:26 AM by Andre Perusse with no comments
    Filed under:
  • Making Reporting Services Work On Vista (64-bit)

    A couple of weeks ago I got a new laptop (that went along with my new job) and at the urging of some on my co-workers (and against my better judgement) I installed Vista Enterprise 64-bit on it. And that's another story all together. I also installed SQL Server 2005 (64-bit, naturally) and after fighting with the new IIS 7 on Vista (see this Microsoft Support Article for more info), I managed to get Reporting Services installed too.

    However, I had never configured it until today. I brought up the Reporting Services Configuration Manager and focused my attention on the first "red X" in the list, which was the "Report Manager Virtual Directory." I was able to set up the virtual directory, but the damn red X wouldn't turn into a green checkmark no matter what I did. I searched the Internet and found all kind of Reporting Services on Vista horror stories, but no story that fit my exact problem. After looking at the various Reporting Services .config files for clues, I went back to the Configuration Manager window and clicked on the "Report Server Virtual Directory" item, which had a green checkmark. Aha! The damn thing WASN'T set up, even though it had a happy green checkmark. Once I set up both the Report Server and Report Manager virtual directories with the proper application pool settings outlined in the above referenced Microsoft Support article, I was able to complete the Reporting Services configuration and now it works great!

    Moral of the story: don't trust green checkmarks - they're not always correct.
Copyright - all rights reserved
Powered by Community Server (Non-Commercial Edition), by Telligent Systems