Tuesday, July 14, 2009

.htaccess RewriteRule in IIS 7.0

I'm working on a big PHP project on my new Vista (YUCK) machine. Sure enough I download the PHP source files and nothing will work on IIS. Its not processing the .htacess file.

This is how I got it to work.

I went to the IIS admin, selected the website that had the issue and in the control panel options. I right clicked in the panel and selected "Install Application from Gallery".


I then selected "PHP v5.-.-" (not pictured), and clicked install. (I already had installed PHP, but why not).

After that finished installing, an option for URL ReWrite 1.1 appeared in the next window. I selected it and installed it.

Once that is installed, select the site that you need to process your .htaccess file and double click the Rewrite URL icon.

A blank list will appear, but an option to import is in the upper right hand corner, click it.

Browse for your .htaccess file and import it. Any rules will be sucked into IIS.

BINGO-- It works.

I suppose I'll have to import it everytime I change it, but it works.

Hope this helps.

Thursday, July 9, 2009

Add time selector to .NET Ajax Control Toolkit 1.1 Calendar Extender



Hey folks,
I recently had a project that called for the additon of a time selector to the Ajax Control Toolkit 1.1 Calendar Extender control . This version is only a 24hr time selector, so no AM - PM, but my next version will include this.

Added Properties:
DateOnly - Boolean: Turns the time selector on and off. Default is true.

Files Changed:
CalendarBehavior.js
CalendarExtender.cs

Getting it working:
Option 1:
  • Download the entire source for the Ajax Control Tool kit from here .
  • Replace the above files under the calendar folder in the AjaxControlTookit project.

Option 2:

Add a reference to the DLL in your project.

Control Behavior:

If DateOnly is true, the time selector will hide.If no date is assigned to the textbox linked to to the control the set button will not show.


Download It

Friday, June 20, 2008

ASP.NET 2.0 Custom Basepage, how and why? (C#)

First off, by a show of hands, who knows what ASP.NET is? Ok.... by another show of hands... Who knows what a custom basepage written in C# is? If you raised your hand you can stay, but if you didn't, stop reading now and move on to another article. There is way too much background needed to get you up to speed.

Now, for those who have stayed. As you know ASP.NET is Microsoft's web application language, built around the .NET framework. And as you probably also know, the term "basepage" was derived from an Object Oriented Programming (OOP) term. "Base class", is a term used to describe the highest level of inheritances within a OOP structured program. In other words, if there was a class called "circle" it probably inherits from the "shape" class. The problem is, that metaphor doesn't translate well to the web world. A web page, is a web page, right? Ok, if you are advanced you will quickly say, "a web page is not just a web page", and you would be right, it can have lots of sub objects associated with it, but for this example we are just looking at the pages as plain pages; the very beginnings of a site.

So, Why BasePage?
BasePages are useful because they allow you to implement functionality globally across a site. This is good for all sorts of reasons, but one would be security. If you have site that is password protected you will want to check to see if the person viewing a particular page has been authenticated, or to ensure their session is valid. This can be done within the BasePage, instead of copying and pasting the same code to every page. (Note: There are these things called HTTP handlers that work like this at a lower lever, but we are not talking about that right now.)

Ok, how do I do it?
When you "Add" a new WebForm.aspx page to a website project in visual studio, it automatically assigns "System.Web.UI.Page" as the default base class. This is convenient. It allows you to start coding immediately (Note: There are ways to override this, not discussed in this article.) . The issue arises when you want to have a particular piece of code fire on every page. Because "System.Web.UI.Page" can not be modified (because its a compiled Assembly), you would have to copy the code, you need to fire on every page, to each page. This is not good. What we need to do is to create a custom BasePage, an intermediary between the "System.Web.UI.Page" and your WebForm.

So instead of this:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}


You have this:
public partial class _Default : CustomBasePage
{
protected Page_Load(object sender, EventArgs e)
{
}
}

So lets get started, how do you create this intermediary class? Well its easy.



  1. Add a new class your website project and call it BasePage.cs.
  2. When the newly created file opens it will look like this:
    ///
    /// Summary description for BasePage
    ///

    public class BasePage
    {
    public BasePage()
    {
    //
    // TODO: Add constructor logic here
    //
    }
    }


  3. To turn this ordinary class into a useful BasePage, your going to have to modify a few things. One is your going to have to inherit from our friend "System.Web.UI.Page". This allows all the meat of that framework Assembly to be available to your web page. This is done by adding a " : " Powell after the class name and adding the Assembly reference. Like this: public class BasePage : System.Web.UI.Page


  4. Next we want to add the page life cycle events that will fire for each page. These are the methods where you will put your code that you want to fire on every page. Like this:

    public class BasePage : System.Web.UI.Page
    {
    public BasePage()
    {
    }
    protected override void OnPreInit(EventArgs e)
    {
    //Add the code you need to fire here
    base.OnPreInit(e);
    }
    protected override void OnLoad(EventArgs e)
    {
    //Add the code you need to fire here.


    Response.Write("Hello");
    base.OnLoad(e);
    }
    }

  5. Now that you have created your BasePage class, change the inherited class on your WebForm to your newly created BasePage.

  6. Compile your website and view it. The word "Hello" should be at the top of the page.

Congratulations...You have just created a BasePage.

Summary

In this article I briefly and generally described the how and why of using a custom BasePage within your website. I hope you find it useful.


Resources: