已安裝 SQLExpress SP2 但要改裝 SQL2005 的方法

今天第一次遇到 SQLExpress 中的資料庫大小超出限制的情況,主要是因為客戶要求所有上傳的檔案都要儲存在 SQL Server 中的緣故,所以資料庫的成長量十分驚人。

而當我想改裝 SQL Server 2005 Developer Edition 時想說連 Management Studio Express 也一併升級到 Management Studio 版本,雖然資料庫安裝十分順利,但安裝 Management Studio 時卻發生無法安裝的狀況,我嘗試將 Management Studio Express 解除安裝後還是無法正常安裝,他還是叫我移除一些東西,不過我實在想不出還要移除哪些程式,因為若是再移除就要移除資料庫了,就想說這應該不太對,我試了好幾次才試出來如何安裝成功。主要原因是因為安裝程式判斷出我現有的主機的用戶端元件是 Service Pack 2 的版本,而嘗試要安裝的工具卻是沒有 Service Pack 的版本(因為剛安裝當然沒有SP2),所以無法執行升級的程序。

要能成功改裝 Management Studio 首先必須到控制台的「新增或移除程式」找到 Microsoft SQL Server 2005 並點選「變更」,接著會出現「Microsoft SQL Server 2005 維護」視窗:

Microsoft SQL Server 2005 維護

選取「工作站元件」後按「下一步」幾次,直到以下畫面出現:

接著點選「變更安裝的元件」

接著點選「變更安裝的元件」並將「用戶端元件」全部移除

將「用戶端元件」全部移除

然後一直按下一步將安裝程式執行完就會將「用戶端元件」完整移除,這時再去安裝 Management Studio 就會安裝成功了。

相關連結

  

此文章由 will 發表於 2008/4/30 上午 12:14:14

永久連結 | 評論 (0) | 此文章的RSSRSS comment feed |

分類: SQL Server

標籤: ,

收藏:

修正 Linux 時區的方式

之前在安裝 Ubuntu 的時候不知道怎麼選的,時區選錯了,導致怎麼校時都不對,且都沒有現成的工具程式可以幫我切換。

其實只有三行指令要下而已,底下的範例是將時區切換到 Asia/Taipei 的時區:

cd /etc
mv localtime localtime.OLD
cp /usr/share/zoneinfo/Asia/Taipei /etc/localtime

調整完時區之後在做一次時間校正就可以了:

root@server:~# ntpdate tick.stdtime.gov.tw
29 Apr 19:09:41 ntpdate[7660]: adjust time server 220.130.158.51 offset 0.447079 sec

相關連結

  

此文章由 will 發表於 2008/4/29 下午 07:06:30

永久連結 | 評論 (0) | 此文章的RSSRSS comment feed |

分類: Linux

標籤:

收藏:

.NET Framework 3.5 在非英語語系無法安裝的問題

今天去客戶那裝機發現 .NET Framework 3.5 無法安裝,主因是 .NET Framework 3.5 在安裝的時候會偵測原作業系統的語系然,如果不是「英文語系」就會動態下載 Language Pack (語言套件) 回來安裝,即便你去下載中文版的 .Net Framework 3.5 安裝檔Microsoft .NET Framework 3.5 完整套件 都一樣,裡面都沒有包含中文語系檔,都需要在安裝過程進行下載的動作。

但客戶端的主機因為安全考量將所有連外的網路全部切斷而導致無法下載,所以每次安裝到一半就卡住了,此時立即上網找到了答案,共有三個步驟要做:

1. 首先必須先到這裡下載 dotNetFx35setup.exe 安裝檔,並執行以下指令解壓縮檔案

dotNetFx35setup.exe /x

2. 假設你解壓縮到 D:\dotnetfx35 目錄下,執行以下指令直接指明安裝「英文版」,就可以跳過動態下載中文語系的問題了。

D:\dotnetfx35\setup.exe /lang:ENU

3. 之後再去下載 Microsoft .NET Framework 3.5 語言套件 並上傳到伺服器另外安裝即可。

相關連結

  

此文章由 will 發表於 2008/4/29 上午 12:02:46

永久連結 | 評論 (0) | 此文章的RSSRSS comment feed |

分類: .Net | 系統管理

標籤:

收藏:

SQL 2000 的 Uniqueidentifier 欄位在 LINQ to SQL 的問題

我有個專案原本都在 SQL Server 2005 上面開發的,到最後才將資料庫轉換到 SQL Server 2000 上,結果在執行 Insert 的時候卻發生 "The primary key column of type 'UniqueIdentifier' cannot be generated by the server. " 錯誤訊息,如下圖:

The primary key column of type 'UniqueIdentifier' cannot be generated by the server.

不過我在「建立時間」欄位上設定 Auto Generated Value 為 True 是可以正常運作的!

看這個樣子應該是 LINQ to SQL 在 SQL Server 2000 資料庫中的 Primary Key 不支援 Auto Generated Value,也就是說我的 LINQ to SQL Class 裡面每一個表格的 Primary Key 定義都必須將 Auto Generated Value 設定為 False ( 也是預設的狀態 ),但為了減少現有程式的修改量,我在每一個表格加上一個 OnCreated() Partial Method,並在這裡指定預設的 Guid 就解決所有問題了。

public partial class Role
{
    partial void OnCreated()
    {
        if (this.ID.CompareTo(new Guid()) == 0)
        {
            this.ID = Guid.NewGuid();
        }
    }
}

LINQ to SQL 的架構真的很直覺,有很多以前要寫很多 Code 的地方現在都簡化了,我也自從開始用 LINQ to SQL 之後,就一直不斷發現一些小地方讓我十分滿意。

  

此文章由 will 發表於 2008/4/28 上午 12:00:44

永久連結 | 評論 (0) | 此文章的RSSRSS comment feed |

分類: .Net | ASP.NET | LINQ | SQL Server

標籤: , , ,

收藏:

Ubuntu 8.04 LTS 如何安裝 PHP4

據我所知 Ubuntu Linux 大概從 6.06 (Dapper) 開始就不支援 PHP4 了,且 PHP 官方網站也宣布從 2007-12-31 起停止了 PHP4 的計畫(也就是以後也不會再出新版),不過我想應該還是有不少網站是用 PHP4 寫的,要讓這些網站在短時間內全部升級改寫成 PHP5 的版本還真不太容易。今天我也把 Ubuntu 8.04 LTS 裝起來了,並嘗試著也將 php4 裝起來,以下是在 Ubuntu 8.04 成功安裝 php4 的心得分享(當然這個方法在 Ubuntu 6.06, 6.10 或 7.10 一樣適用)。

1. 建立一個檔案到 /etc/apt/sources.list.d/ 目錄下:

# vi /etc/apt/sources.list.d/dapper.sources.list

   檔案內容僅需要輸入一行即可:

deb http://tw.archive.ubuntu.com/ubuntu/ dapper universe main restricted multiverse

2. 執行 apt-get update 指令更新 Packages 資料庫

root@ubuntu804:~# apt-get update
Hit http://tw.archive.ubuntu.com hardy Release.gpg
Hit http://tw.archive.ubuntu.com hardy-updates Release.gpg
Hit http://tw.archive.ubuntu.com dapper Release.gpg
Hit http://tw.archive.ubuntu.com hardy Release
Hit http://tw.archive.ubuntu.com hardy-updates Release
Hit http://tw.archive.ubuntu.com dapper Release
Hit http://tw.archive.ubuntu.com hardy/main Packages
Hit http://tw.archive.ubuntu.com hardy/restricted Packages
Hit http://tw.archive.ubuntu.com hardy/main Sources
Hit http://tw.archive.ubuntu.com hardy/restricted Sources
Hit http://tw.archive.ubuntu.com hardy/universe Packages
Hit http://tw.archive.ubuntu.com hardy/universe Sources
Hit http://tw.archive.ubuntu.com hardy/multiverse Packages
Hit http://tw.archive.ubuntu.com hardy/multiverse Sources
Hit http://tw.archive.ubuntu.com hardy-updates/main Packages
Hit http://tw.archive.ubuntu.com hardy-updates/restricted Packages
Hit http://tw.archive.ubuntu.com hardy-updates/main Sources
Hit http://security.ubuntu.com hardy-security Release.gpg
Hit http://tw.archive.ubuntu.com hardy-updates/restricted Sources
Hit http://security.ubuntu.com hardy-security Release
Hit http://security.ubuntu.com hardy-security/main Packages
Hit http://tw.archive.ubuntu.com hardy-updates/universe Packages
Hit http://security.ubuntu.com hardy-security/restricted Packages
Hit http://security.ubuntu.com hardy-security/main Sources
Hit http://security.ubuntu.com hardy-security/restricted Sources
Hit http://tw.archive.ubuntu.com hardy-updates/universe Sources
Hit http://security.ubuntu.com hardy-security/universe Packages
Hit http://security.ubuntu.com hardy-security/universe Sources
Hit http://security.ubuntu.com hardy-security/multiverse Packages
Hit http://security.ubuntu.com hardy-security/multiverse Sources
Hit http://tw.archive.ubuntu.com hardy-updates/multiverse Packages
Hit http://tw.archive.ubuntu.com hardy-updates/multiverse Sources
Hit http://tw.archive.ubuntu.com dapper/universe Packages
Hit http://tw.archive.ubuntu.com dapper/main Packages
Hit http://tw.archive.ubuntu.com dapper/restricted Packages
Hit http://tw.archive.ubuntu.com dapper/multiverse Packages
Reading package lists... Done

   你可以發現最後面取得的時 dapper/universe 套件,這裡面就有包括 php4 的所有相關套件。

3. 搜尋所有 php4 相關的套件

root@ubuntu804:~# apt-cache search php4
apache2-prefork-dev - development headers for apache2
cakephp - MVC rapid application development framework for PHP
cakephp1.2 - MVC rapid application development framework for PHP (1.2 version)
caudium - An extensible WWW server written in Pike
dtc - web control panel for admin and accounting hosting services
libphp-phplot - The graphic library for PHP
php-image-canvas - Image_Canvas module for PEAR
php-image-graph - Image_Graph module for PEAR
php5-syck - YAML parser kit -- PHP5 bindings
php-doc - Documentation for PHP4 and PHP5
dbtcp - Miscellaneous command-line DBTCP utils
fibusql - Web based double-entry accounting
libapache-mod-php4 - server-side, HTML-embedded scripting language (apache 1.3 module)
libapache2-mod-php4 - server-side, HTML-embedded scripting language (apache 2.0 module)
odontolinux - Dental office management software (PHP4 + PostgreSQL)
php4 - server-side, HTML-embedded scripting language (meta-package)
php4-apd - PHP code execution profiler and debugger
php4-auth-pam - A PHP extension for PAM authentication
php4-cgi - server-side, HTML-embedded scripting language (CGI binary)
php4-clamavlib - PHP ClamAV Lib - ClamAV Interface for PHP4 Scripts
php4-cli - command-line interpreter for the php4 scripting language
php4-common - Common files for packages built from the php4 source
php4-curl - CURL module for php4
php4-dbtcp - PHP bindings for DBTCP
php4-dev - Files for PHP4 module development
php4-domxml - XMLv2 module for php4
php4-gd - GD module for php4
php4-gpib - libgpib php bindings
php4-idn - PHP api for the IDNA library
php4-imagick - ImageMagick module for php4
php4-imap - IMAP module for php4
php4-interbase - interbase/firebird module for php4
php4-json - json serialiser for PHP4
php4-kadm5 - An extension to manage kerberos admin information
php4-lasso - Liberty ID-FF library - PHP 4 bindings
php4-ldap - LDAP module for php4
php4-mapscript - module for php4-cgi to use mapserver
php4-maxdb - PHP extension to access MaxDB databases
php4-mcal - MCAL calendar module for php4
php4-mcrypt - MCrypt module for php4
php4-mhash - MHASH module for php4
php4-mysql - MySQL module for php4
php4-odbc - ODBC module for php4
php4-pear - PHP Extension and Application Repository (transitional package)
php4-pear-log - Log module for PEAR
php4-pgsql - PostgreSQL module for php4
php4-ps - An extension to create PostScript files
php4-pspell - pspell module for php4
php4-recode - Character recoding module for php4
php4-rrdtool - RRD module for php4
php4-snmp - SNMP module for php4
php4-spplus - Secured payment system of the Caisse d'Epargne (French bank)
php4-sqlite - PHP4 bindings to SQLite, a file-based SQL engine
php4-sqlrelay - SQL Relay PHP API
php4-sybase - Sybase / MS SQL Server module for php4
php4-tclink - TrustCommerce TCLink module for php4
php4-uuid - OSSP uuid module for php4
php4-xslt - XSLT module for php4
php4-yaz - php/yaz z39.50 module
phpgroupware-felamimail - phpGroupWare felamimail (Squirrelmail) module

4. 安裝 php4-cli

root@ubuntu804:~# apt-get install php4-cli
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  libdb4.3 libpcre3 libzzip-0-12 php4-common
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libdb4.3 libpcre3 libzzip-0-12 php4-cli php4-common
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 0B/2389kB of archives.
After this operation, 5018kB of additional disk space will be used.
Do you want to continue [Y/n]?
Selecting previously deselected package libdb4.3.
(Reading database ... 16202 files and directories currently installed.)
Unpacking libdb4.3 (from .../libdb4.3_4.3.29-11ubuntu1_i386.deb) ...
Selecting previously deselected package libpcre3.
Unpacking libpcre3 (from .../libpcre3_7.4-1ubuntu2_i386.deb) ...
Selecting previously deselected package libzzip-0-12.
Unpacking libzzip-0-12 (from .../libzzip-0-12_0.12.83-5_i386.deb) ...
Selecting previously deselected package php4-common.
Unpacking php4-common (from .../php4-common_4%3a4.4.2-1build1_i386.deb) ...
Selecting previously deselected package php4-cli.
Unpacking php4-cli (from .../php4-cli_4%3a4.4.2-1build1_i386.deb) ...
Setting up libdb4.3 (4.3.29-11ubuntu1) ...
Setting up libpcre3 (7.4-1ubuntu2) ...

Setting up libzzip-0-12 (0.12.83-5) ...

Setting up php4-common (4:4.4.2-1build1) ...
Setting up php4-cli (4:4.4.2-1build1) ...

Processing triggers for libc6 ...
ldconfig deferred processing now taking place

5. 大功告成!我們來測試一下 php4 是否可以正常執行:

root@ubuntu804:~# php4 -v
PHP Warning:  mime_magic: type search/400       \\input         text/x-tex invalid in Unknown on line 0
PHP Warning:  mime_magic: type search/400       \\section       text/x-tex invalid in Unknown on line 0
PHP Warning:  mime_magic: type search/400       \\setlength     text/x-tex invalid in Unknown on line 0
PHP Warning:  mime_magic: type search/400       \\documentstyle text/x-tex invalid in Unknown on line 0
PHP Warning:  mime_magic: type search/400       \\chapter       text/x-tex invalid in Unknown on line 0
PHP Warning:  mime_magic: type search/400       \\documentclass text/x-tex invalid in Unknown on line 0
PHP Warning:  mime_magic: type regex            [Cc]onstant[[:space:]]+[Ss]tory text/x-inform invalid in Unknown on line 0
PHP 4.4.2-1build1 (cli) (built: Apr  6 2006 09:44:32)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

   你可以發現執行的過程會出現一堆 PHP Warning 的警告訊息,這是因為 Ubuntu 8.04 中預設的 /usr/share/file/magic.mime (MIME 定義檔) 檔案中有幾行 php4 認不得,而導致出現警示訊息,不過這幾行並不會影響 PHP 的運作。

   因為在 magic.mime 中的這 7 行是用來定義 TeX documents 與 Inform interactive fiction language 的檔案類型,因為很少用到,所以我覺得將這幾行註解掉應該沒關係!所以我們還是修改一下 magic.mime 檔案,將這幾行「太新」的 MIME 定義給註解掉,讓 php4 不會出現警告訊息。你開啟 /usr/share/file/magic.mime 檔案後直接跳到第598行:

# TeX documents, from Daniel Quinlan (quinlan@yggdrasil.com)
0       search/400      \\input         text/x-tex
0       search/400      \\section       text/x-tex
0       search/400      \\setlength     text/x-tex
0       search/400      \\documentstyle text/x-tex
0       search/400      \\chapter       text/x-tex
0       search/400      \\documentclass text/x-tex

# Type: Inform interactive fiction language
# URL:  http://www.inform-fiction.org/
# From: Reuben Thomas <rrt@sc3d.org>
0       regex           [Cc]onstant[[:space:]]+[Ss]tory text/x-inform

這要將這幾行最前面加上井字號 ( # ) 後存檔,然後我們在測試一下 php4 是否正常執行:

root@ubuntu804:~# php -v
PHP 4.4.2-1build1 (cli) (built: Apr  6 2006 09:44:32)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

完成 php4 安裝!

相關連結

  

此文章由 will 發表於 2008/4/27 上午 12:05:16

永久連結 | 評論 (0) | 此文章的RSSRSS comment feed |

分類: Linux

標籤: , , ,

收藏:

介紹好用工具:IECapt

我之前就介紹過一個 URL2JPEG 工具,但最近又發現一個更棒的工具:IECapt

IECapt 是一個指令列工具(command-line utility),他可以透過 IE 擷取特定網頁的畫面下來並轉存成 BMP, JPEG, PNG, TIFF 或 EMF 等圖片格式,此工具軟體也是一個 Open Source 的專案,你可以下載 C++ 或 C# 的原始碼回來研究喔。有了這個工具就可以實做出動態擷取網頁畫面的功能,目前網路界最有名的畫面預覽網站就是 Snap Shots 網站。

我自己下載回來試了一下,如果要能夠編譯成功大致上有三個步驟,如下:

  1. 新增一個 Console Application 專案
  2. 加入參考(共四個命名空間)
    1. AxSHDocVw
    2. Microsoft.mshtml
    3. System.Drawing
    4. System.Windows.Forms
  3. 開啟 Program.cs 並將內容全部替換成 IECapt.cs 的內容

編譯成功後在 bin\Debug 目錄下就會出現 IECapt.exe 可以用了,預設 C# 版編譯出來的使用方式如下:

Usage: IECapt --url=http://... --out=file.png

Options:
  --url         The URL to capture
  --out         The target file (.png|jpeg|bmp|emf|tiff)
  --min-width   Minimal width for the image (default: 800)
  --delay       Capturing delay in ms (default: 1)

如果要擷取網頁圖片可以這樣用:

IECapt --url=http://www.miniasp.com/ --out=miniasp.png

輸出的結果會變成底下這張圖:

多奇數位創意有限公司

你可以看到上面黑黑的一塊其實是 Flash 剛開始跑的畫面,你如果要等待多幾秒再擷取畫面的話,可以加上 --delay 參數等待個五秒:

IECapt --url=http://www.miniasp.com/ --out=miniasp.png --delay=5000

擷取到的畫面變成這樣:

多奇數位創意有限公司

是不是完美多了呢! ^_^

我是覺得參數太少了,不夠用就自己加摟,特別是他抓下來的畫面是要多大有多大,像從我的部落格首頁下載下來網頁圖片的高度就有 17,104px,如果要限制圖片的大小就必須改原始碼了,大概在原始碼的第 951 行的地方有個 DoCapture() 方法,裡面有定義擷取畫面的寬(width)、高(height),如果你要限制畫面輸出的大小只要修改這一段的程式即可。

相關連結

  

此文章由 will 發表於 2008/4/26 上午 12:01:45

永久連結 | 評論 (0) | 此文章的RSSRSS comment feed |

分類: 介紹好用工具

標籤: ,

收藏:

SQL Server 2005 如何複製資料庫

前幾天在開發專案的時候需要複製一個資料庫用來做測試,但之前每次都手動做備份、建立新資料庫、還原資料等動作還蠻費時的,因此心血來潮寫了一段「複製資料庫」的 T-SQL 幫我執行複製資料庫的工作,自己覺得還含蠻好用的,T-SQL 程式如下:

-- 宣告來源資料庫的名稱
DECLARE @Source_DB nvarchar(256);
-- 宣告來源資料庫的「資料邏輯名稱」
DECLARE @Source_DB_LogicalName_Dat nvarchar(256);
-- 宣告來源資料庫的「紀錄邏輯名稱」
DECLARE @Source_DB_LogicalName_Log nvarchar(256);

-- 宣告目的地的資料庫的名稱
DECLARE @Target_DB nvarchar(256);

-- 取得資料庫本機的 DATA 資料目錄
DECLARE @data_path nvarchar(256);
DECLARE @data_file_mdf nvarchar(256);
DECLARE @data_file_ldf nvarchar(256);
SET @data_path = (SELECT SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1)
                  FROM master.sys.master_files
                  WHERE database_id = 1 AND file_id = 1);

-- 取得資料庫本機的 Backup 資料目錄
DECLARE @backup_path nvarchar(256);
DECLARE @backup_file nvarchar(256);
SET @backup_path = (SELECT SUBSTRING(SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1), 1, CHARINDEX(N'\DATA\', LOWER(physical_name)) - 1) + '\Backup\'
                  FROM master.sys.master_files
                  WHERE database_id = 1 AND file_id = 1);
----------------
-- 參數設定區 --
----------------
SET @Source_DB = 'MyDB';
SET @Source_DB_LogicalName_Dat = @Source_DB
SET @Source_DB_LogicalName_Log = @Source_DB + '_log';
SET @Target_DB = 'MyDB_TEMP';

SET @backup_file = @backup_path + @Source_DB + '.bak';

SET @data_file_mdf = @data_path + @Target_DB + '.mdf';
SET @data_file_ldf = @data_path + @Target_DB + '_log.ldf';

-- 先將 @Source_DB 資料庫備份至 Backup 資料夾
BACKUP DATABASE @Source_DB 
   TO DISK=@backup_file;

-- 最後將傳回的資料記錄檔還原到 @Target_DB 資料庫
RESTORE DATABASE @Target_DB
   FROM DISK=@backup_file 
   WITH MOVE @Source_DB_LogicalName_Dat TO @data_file_mdf,
   MOVE @Source_DB_LogicalName_Log TO @data_file_ldf;
GO

使用說明:

  • 基本上只需要修改「參數設定區」的 @Source_DB 與 @Target_DB 即可成功複製資料庫。
    @Source_DB : 來源資料庫
    @Target_DB : 目的資料庫 ( 即新的資料庫 )
  • 如果你原本的資料庫邏輯名稱不是預設的話,你要自己修改 @Source_DB_LogicalName_Dat 與 @Source_DB_LogicalName_Log 參數。
  • 如果你的資料庫中已經有 @Target_DB 所定義的這個資料庫名稱的話,所有資料都會被 @Source_DB 資料庫中的資料直接覆蓋掉喔!!
  

此文章由 will 發表於 2008/4/25 上午 09:36:41

永久連結 | 評論 (0) | 此文章的RSSRSS comment feed |

分類: SQL Server

標籤: ,

收藏:

Linux 下的目錄捷徑設定

大部分來說 Linux 下的 "目錄捷徑" 通常會用 symbolic links,不過使用 symbolic links 有個小問題,就是使用 FTP 連線進來的使用者通常無法透過 symbolic link 跳到特定目錄(因為 chroot 的關係),而這時又想開放特定目錄給使用者的話,就可以用今天介紹的小技巧。

一般來說使用 symbolic link 的指令是:

# ln -s /var/www/logs /home/myhome/logs

若你想開放系統中某一個特定的目錄給特定使用者讀取資料,可以用掛載(mount)的方式將特定目錄連結至使用者的目錄下,以下是掛載目錄的範例與大致說明:

1. 首先必須先建立好掛載的目錄才能使用 mount 將目錄掛上去

# mkdir /home/myhome/logs

2. 接下來就是將指定的目錄掛上去了

# mount --bind /var/www/logs /home/myhome/logs

這個時候的 /home/myhome/logs 目錄的權限與使用者就會跟 /var/www/logs 一模一樣了,可以保有原本目錄、檔案的安全性,也不會讓使用者在系統目錄間到處亂逛了。

  

此文章由 will 發表於 2008/4/24 上午 09:27:47

永久連結 | 評論 (0) | 此文章的RSSRSS comment feed |

分類: Linux | Tips

標籤: ,

收藏:

介紹好網站:MiniAjax.com

MiniAjax.com / A showroom of nice looking simple downloadable DHTML and AJAX scripts

有一個小巧玲瓏的網站 MiniAjax.com,裡面收錄了 62 個好用的 Ajax 小元件(陸續增加中...),我自己親身試用了幾個發現還真不錯呢,另外也有些 Ajax Library 功能真的很酷!

例如說有個可以偵測你網站中大部分人都會在你的網站點選哪些位置,並且用顏色標注出來:

ClickHeat is a visual heatmap of clicks on a HTML page, showing hot and cold click zones.

或者有個圖片放大鏡的工具

Makes large images completely visibly through small page-areas.

還有很多呢,上去看就知道了。

該網站也有提供 RSS 有興趣的人可以去訂閱,只要有新出爐的 Ajax 元件你就可以馬上知道喔。

相關連結

  

此文章由 will 發表於 2008/4/23 上午 01:36:00

永久連結 | 評論 (1) | 此文章的RSSRSS comment feed |

分類: JavaScript | Web

標籤: , ,

收藏:

利用 ASP.NET 2.0 的 Control Adapter 架構實做「刪除」按鈕

在設計管理介面時,一定會做「刪除」功能,我想很多人都會在「刪除」按鈕加上一個 JavaScript 確認的程式,以確保使用者沒有誤觸刪除按鈕而導致資料直接被刪除。

對一個普通的按鈕來說,要加上「刪除確認」的程式是很容易的,只要在 Button 控制項的 OnClientClick 屬性加上 confirm() 函數就可以做倒刪除確認的功能:

<asp:Button ID="mDelete_Button" runat="server" Text="刪除"
    OnClientClick="if(!confirm('你確定要刪除嗎?')) return false;"  />

而對於最常用需要用到刪除功能的地方不外乎是 GridView、DetailsView 或 FormView 控制項中,如果你要在這些控制項裡增加「刪除」功能是很容易的,一般來說會有兩種作法:

第一種:新增一個 CommandField 宣告,就可以新增一個刪除的按鈕:

<asp:CommandField ButtonType="Button" ShowDeleteButton="true"
    DeleteText="刪除" HeaderText="刪除" />

但是第一種作法很不容易加上 OnClientClick 屬性,以 GridView 控制項為例,真的要替 CommandField 的刪除按鈕加上 onclick 屬性必須實做 RowDataBound 事件,並在事件中找到這個 Button 控制項 ( 如果你 CommandField 中的 ButtonType 屬性設定為 Button 的話 ),再手動加入 onclick 屬性(Attribute),這真是太麻煩了。

第二種:新增一個 TemplateField 宣告,並自己新增 Button 控制項進去,有經驗的人就知道只要將 CommandName 設定成 Delete 就可以讓這個按鈕跟使用 CommandField 的刪除功能一模一樣,其實 CommandField 輸出的控制項就是將 CommandName 設定為 Delete。如果要加上刪除確認功能,這時就不需要再寫 Code Behind 的程式了,直接在 *.aspx 頁面的刪除按鈕加上 OnClientClick 屬性就好了,就如同本篇文章最上面的那個範例一樣。

<asp:TemplateField>
    <ItemTemplate>
        <asp:Button ID="mDelete_Button" runat="server" CommandName="Delete"
Text="刪除" OnClientClick="if(!confirm('你確定要刪除嗎?')) return false;" /> </ItemTemplate> </asp:TemplateField>

不過,我今天要講的技巧更厲害,是透過 ASP.NET 2.0 的 Control Adapter 機制,直接設定「全站」所有的 Button 控制項只要遇到 CommandName 為 Delete 的,就自動加上 OnClickClick 屬性進行刪除確認的功能。

首先,你必須在你的網站專案加上 App_Browsers 目錄,並新增一個 ControlAdapters.browser 檔案 (檔名可以改,副檔名不能改),並輸入以下內容:

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.WebControls.Button" adapterType="ButtonAdapter" />
      <adapter controlType="System.Web.UI.WebControls.LinkButton" adapterType="ButtonAdapter" />
      <adapter controlType="System.Web.UI.WebControls.ImageButton" adapterType="ButtonAdapter" />
    </controlAdapters>
  </browser>
</browsers>

這裡我定義了 3 個不同的按鈕控制項配置器(Control Adapter),並且都指定到 ButtonAdapter 類別。

然後再到 App_Code 目錄下新增一個  ButtonAdapter.cs 類別檔,程式也很短,如下:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
public class ButtonAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter {
    protected override void OnPreRender(EventArgs e)
{ IButtonControl btn = Control as IButtonControl; // mDelete 是我自訂的 CommandName 名稱,你也可以改成你自己的名稱 if (btn.CommandName == "Delete" || btn.CommandName == "mDelete") { Control.Attributes["onclick"] = "if(!confirm('你確定要刪除嗎?')) return false;"; } base.OnPreRender(e); } }

這樣就做完了!

整個網站完全不需要再特別定義 OnClientClick="if(!confirm('你確定要刪除嗎?')) return false;" 指令,只要你的 CommandName 設定成 Delete 或 mDelete 都會自動加上 onclick 屬性,是不是一勞永逸呢!^_^

ASP.NET 2.0 的 Control Adapter 架構是我非常喜歡的一個架構,他可以針對不同的 Browser 以及針對不同的控制項進行客製化的修正,也可以用在像我今天講的這個開發技巧上,真的可以將程式碼的量降低,也降低專案的複雜度。

如果有興趣瞭解 Control Adapter 架構的人可以下載 ASP.NET 2.0 CSS Friendly Control Adapters 1.0 範例回來好好研究一下,相信會有更多的創意出現,記得回來跟我分享喔。:-)

相關連結

  

此文章由 will 發表於 2008/4/22 上午 12:01:45

永久連結 | 評論 (1) | 此文章的RSSRSS comment feed |

分類: ASP.NET

標籤: , , ,

收藏: