Canceling AutoPostBack for Asp.Net DropDownList

In my most recent project, I decided to prompt the user before doing a post back when a DropDownList changed. In order to do so, you need to add an “onchange” event to your DropDownList. It should look something like:

onclick="if(!SomeFunction(this)) return false;"

Some Notes:

  • “SomeFunction” is a javascript function that returns true or false based on your criteria.
  • On first inspection, I thougth that I could just call the funciton without wrapping it in what apppears to be a useless IF statement. However, for whatever reason, this IF statement is necessary to cancel the automatic postback.

Reference: http://forums.asp.net/p/1475520/3432980.aspx

–Adam

Simple Copy Files & Folders Script in Python

As I’ve mentioned before, I use Microsoft’s Windows Home Server as my home’s central data store. Recently, I upgraded from the trial to the full version. Since I was going to upgrade the installation, I figured that I might as well (slightly) upgrade the server itself. As such, I wanted to copy off the data from the hard drives before doing a format & reinstall of WHS. To do so, I accessed the “hidden” DE folder located on each of my WHS hard drives and copied the data to my local machine. When the data was transferred, I formatted the drive and re-added it to my new WHS install. However, when I started to transfer the data back, I noticed that many folders were scattered across multiple hard drives. Rather than manually merging the folders back together, I decided to write this simple Python script.

[2012-08-14 update: I’ve added a Python 3 compatible script (minor changes needed)]

Python 2

import os
import shutil
sourcePath = r'E:\some\path'
destPath = r'E:\some\other\path'
for root, dirs, files in os.walk(sourcePath):

    #figure out where we're going
    dest = destPath + root.replace(sourcePath, '')

    #if we're in a directory that doesn't exist in the destination folder
    #then create a new folder
    if not os.path.isdir(dest):
        os.mkdir(dest)
        print 'Directory created at: ' + dest

    #loop through all files in the directory
    for f in files:

        #compute current (old) & new file locations
        oldLoc = root + '\\' + f
        newLoc = dest + '\\' + f

        if not os.path.isfile(newLoc):
            try:
                shutil.copy2(oldLoc, newLoc)
                print 'File ' + f + ' copied.'
            except IOError:
                print 'file "' + f + '" already exists'

Python 3

import os
import shutil
sourcePath = r'D:\some\path'
destPath = r'D:\some\other\path'
for root, dirs, files in os.walk(sourcePath):

    #figure out where we're going
    dest = destPath + root.replace(sourcePath, '')
    
    #if we're in a directory that doesn't exist in the destination folder
    #then create a new folder
    if not os.path.isdir(dest):
        os.mkdir(dest)
        print('Directory created at: ' + dest)

    #loop through all files in the directory
    for f in files:

        #compute current (old) & new file locations
        oldLoc = root + '\\' + f
        newLoc = dest + '\\' + f

        if not os.path.isfile(newLoc):
            try:
                shutil.copy2(oldLoc, newLoc)
                print('File ' + f + ' copied.')
            except IOError:
                print('file "' + f + '" already exists')

–Adam