2007年7月20日 星期五

瘋狂的點子

http://www.scaryideas.com/


BookPlus: A book will never let you down
BookPlus Radio Sport: Who said men can't multi-task? Can't multi-task? Apple T-Shirts from the '80s
Apple '80sTshirts



BookPlus: A book will never let you down
BookPlus Sony PSP: Fake book - The Divine Comedy PSP: Fake book
Neunkircher Zoologischen Garten: The Croc-eats-dog Promotion
Croc-eats-dog



MacGyver Multitool
MacGyver Mumbai Traffic Police: Blood Coasters Bloody Coasters Capriccio
Capriccio



Guinness: Balancing Glasses
Guinness: Balance Wonderbra Wonder Bra
Chopstix: You can't hate everything
Chopstix Wonderbra Wonder Bra
Wonderbra
Wonder Bra Wonderbra Wonder Bra



Chopstix: You can't hate everything
Chopstix Wonderbra Wonder Bra Manix Gel Manix Gel


使用 iTextSharp 做PDF套版

iTextSharp 是一套讓.Net可以處理PDF 的 library,它是從java 版的 iText衍生過來的,官方網站在http://itextsharp.sourceforge.net/ , 下面的範例示範如何讀取當範本的PDF,然後在上面加上中文字及Barcode39 。

值得注意的是 PDF 的座標系統跟螢幕是不一樣的,PDF 是從左下開始算,而螢幕是從左上開始算,只是我目前還不曉得 SetTextMatrix(x,y) 這個Method其中的x,y單位是什麼,另外範例中的這個 PDF範本,有一個很奇怪的地方,它的座標原點並不是在左下角,而是在文件中間?????

using System;
using System.IO;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace PDF1
{

/// <summary>
/// Class1 的摘要描述。
/// </summary>
class Class1
{
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此加入啟動應用程式的程式碼
//
PdfReader reader = new PdfReader("c:\\960621officer_fee.pdf");
int n = reader.NumberOfPages;
PdfStamper stamp = new PdfStamper(reader, new FileStream("c:\\sample2.pdf", FileMode.OpenOrCreate));

PdfContentByte over;
BaseFont bfChinese = BaseFont.CreateFont("C:\\windows\\fonts\\KAIU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

over = stamp.GetOverContent(1);
over.BeginText();
over.SetFontAndSize(bfChinese, 18);
over.SetTextMatrix(-325, -340);
over.ShowText("ˇ");

over.SetFontAndSize(bfChinese, 26);
over.SetTextMatrix(-210, 145);
over.ShowText("游 錫 堃");

over.SetFontAndSize(bfChinese, 18);
over.SetTextMatrix(33, 148);
over.ShowText("A 1 2 0 4 3 5 X X X");
over.EndText();

Barcode39 barcode = new Barcode39();
barcode.Code = "0123456789";
barcode.PlaceBarcode(over,Color.BLACK,Color.BLUE);

// over.SetLineWidth(1f);
// over.SetColorStroke(Color.ORANGE);
// over.MoveTo(-320,-335);
// over.LineTo(-300, -335);
// over.Stroke();

stamp.Close();

}
}
}


2007年7月18日 星期三

用一張圖來解釋 Class ,Object, Instace 之間的關係

http://www.asp101.com/articles/sample_chapters/sitepoint_byoaspnet/chapter3.asp
Classes
You can think of a class as a template for building as many objects as you like of a particular type. When you create an instance of a class, you are creating an object of that class, and the new object has all the characteristics and behaviors (properties and methods) defined by the class.In our dog example, Rayne was an instance of the Dog class as shown in Figure 3.4.




2007年7月13日 星期五

使用Javascript複製 HTML格式的資料到剪貼簿

在工作上有遇到一個需求,就是要將網頁上的部分內容,讓使用者剪貼到Blog的HTML 編輯器,

原本很單純的想法就是使用 :
window.clipboardData.setData("Text", "HTML code goes here");

將網頁內容的HTML Copy 到 clipboard,但後來測試才發現這樣貼到 Blog的HTML 編輯器後,只是顯示出HTML 原始碼而已,而不是我要的網頁內容,google 了一下發現 window.clipboardData.setData 的第一個參數只有 "Text" 跟 "URL" 這兩種,並不支援 Copy HTML 格式的資料到剪貼簿,後來是試了好一陣子,終於找到下面的方法:

function CopyContent(sContenID)
{
var ctrlRange = document.body.createControlRange();
ctrlRange.add(document.all(sContenID));
ctrlRange.execCommand("Copy");
alert("已複製到剪貼簿");
}

這個function 可以將傳進來的的 tag 裡面的內容,用 HTML format copy到 Clipboard ,但有以下的限制:

  • 只能在IE 使用 :(
  • 如果該 tag 是 那要設成 可編輯才可以
  • <a href="http://www.blogger.com/...">是不能傳進去的

2007年7月10日 星期二

解決 window.open 開新視窗時, 原來的視窗會置換成 [object] 的問題

之前的同事遇到當User 點連結,新開一個視窗後,原本的視窗會被置換成只出現[object]的網頁,他原始的Tag 如下:

<a href="javascript:window.open('http://www.microsoft.com');">Click
here</a>

 



解決之道就是在後面加上 void(0)

<a href="javascript:window.open('http://www.microsoft.com');void(0);">Click
here</a>

 



原來window.open 會回傳一個window object,所以當User 點連結後,原本的視窗會被置換成只出現[object]的網頁,只要後面加上 void(0),告訴browser沒有回傳值,這樣就解決了。

請參考:

提供各種DB Connections String 範例的網站

http://www.connectionstrings.com/

2007年7月6日 星期五

Steorn 's Free energy device ORBO

Steorn 這家公司原本預計在7/4展出他們的永動機 ORBO ,但時間到了之後,卻對外宣稱他們的展示機出了一些技術上的問題,所以要延後公開展示,但到了今天(7/6),還是沒看到,等待了快一年的時間,居然是這樣的結果:(

請參考:

GridView 有效率的分頁設計(asp.net)

ASP.NET 的GridView提供很方便的分頁功能,但對於龐大的資料,User 每瀏覽一頁就要到資料庫將所有的資料撈回前端,再進行分頁顯示分頁結果,這樣對效能影響很大,雖然可以設定Datasource的Cache,讓分頁時不用到資料庫撈,直接傳Cache讀,但相對的也會占用的WebServer的記憶體,比較好的方法就是由Stored Procedure 進行分頁,僅傳回該頁的資料,這樣就可以大大提高效能。

Stored Procedure 進行分頁的方法如果是在 SQL Server 2000 可以用 Temp table or table variant , 另外可以利用 Set row count 的方法提高效能。
http://www.4guysfromrolla.com/webtech/042606-1.shtml

至於 SQL Server 2005 就不用這麼麻煩,因為有新的ROW_NUMBER function

CREATE PROCEDURE dbo.ShowLog

@PageIndex INT,
@PageSize INT
AS
BEGIN
WITH LogEntries AS (
SELECT ROW_NUMBER() OVER (ORDER BY Date DESC)AS Row, Date, Description
FROM LOG)
SELECT Date, Description
FROM LogEntries
WHERE Row between (@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize
END


至於ASP.NET 端就要配合 objectDataSource 不能用 SqlDataSource,因為如果用 SqlDataSource就不能用GridView 的分頁功能,而要自己寫分頁,使用objectDataSource時你可以自己寫business object , 或是使用 V.S. 2005 的 Dataset Designer , 它會幫你產生 TableAdapters 讓你可以用在 objectDataSource。

你需要設定 objectDataSource 的

  • EnablePaging = true
  • MaximumRowsParameterName
  • startRowIndexParameterName
  • SelectCountMethod
  • SelectMethod

只是這裡有一個效能的考量,因為 objectDataSource 會先call SelectCountMethod 取得 Page 的 資料,再 call SelectMethod 取得總共有多少筆資料,這樣它才有辦法算頁數,只是這樣就會有兩次的 資料庫連線,如果我們在SelectCountMethod就已經傳回 總共有多少筆,有辦法用這個資料嗎?

答案是有的,只是要用的.net framework 2.0 partial class 的功能,去擴充TableAdapters ,如下的範例:

namespace MyDataSetTableAdapters{
///


/// Summary description for MyDataSetTableAdapters
///

///
public partial class usp_Select_Netatm_logTableAdapter:Component {
private int _totalRowCount = 0;
public int TotalRowCount {
get {
return _totalRowCount;
}
set
{
_totalRowCount = value;
}
}
public int SelectCommandTimeout
{
get
{
return (this._commandCollection[0].CommandTimeout);
}
set
{
for (int i = 0; i < commandtimeout =" value;"> startRowIndex, System.Nullable maximumRows, string IcpNo, DateTime QryFromDate, DateTime QryToDate, ref System.Nullable totalRows)
{
//return ((object)HttpContext.Current.Session["TotalRowCount"]);
return (object)_totalRowCount;
}
}
}

在網頁按Enter時如何觸發button click event (asp.net)

今天在測試時意外發現,當User在網頁按Enter時,是不會觸發button click event 的,google 了一下,發現這個網頁
http://www.beansoftware.com/ASP.NET-Tutorials/Accept-Enter-Key.aspx

重點就是在ASP.NET 2.0 form or panel 有提供一個 defaultbutton 這個屬性,只要設定 defaultbutton ="buttonYouWant" ,當User在網頁按Enter時,就會觸發button click event 。





 <form defaultbutton="button1" runat="server">
    <asp:textbox id="textbox1" runat="server"/>
    <asp:textbox id="textbox2" runat="server"/>
    <asp:button id="button1" text="Button1" runat="server"/>

    <asp:panel defaultbutton="button2" runat="server">
        <asp:textbox id="textbox3" runat="server"/>
        <asp:button id="button2" runat="server"/>
    </asp:panel>

</form>