解決FireFox不支援event.keycode方法

2013年5月8日 星期三

偵測鍵盤上key入判斷,通常在ie上用window.event,但是這個方法在FireFox上卻不支援

這就是開發網頁工程師的困擾了,為什麼這麼多瀏覽器不統一規格呢?

就連IE6~10,各版本會有不相容的問題..Orz

回歸正題啦,加入一下判斷就可以啦!!

 

function onKeyPressBlockNumbers(e)
{
var key = window.event ? e.keyCode : e.which;
var keychar = String.fromCharCode(key);
reg = /\d/;
return !reg.test(keychar);
}

Read more...

啟用方案總管中追蹤現用項目方法

2013年5月7日 星期二

 

 

開啟追蹤現有項目工具=>選項=>專案和方案=>勾選在方案總管中追蹤現用項目 image_20

Read more...

C#存取修飾詞

2013年4月23日 星期二

宣告 定義
public 無存取能力限制
private 存取能力限制於同一類別(Class)內
internal 存取能力限制於同一組件(Project)內
protected 存取能力限制於同一類別或是繼承的類別
protected internal 存取能力限制於同一類別、繼承的類別,或是同一組件內的類別

 

如何使用呢?

型別 型別可宣告修飾詞 成員可宣告修飾詞
class ‧public
‧internal
‧public
‧private
‧internal
‧protected
‧protected internal
struct ‧public
‧internal
‧public
‧private
‧internal
enum ‧public
‧internal
‧不使用
interface ‧public
‧internal
‧不使用
delegate ‧public
‧internal
‧不使用

預設值

宣告 預設值
命名空間 public
類別 internal
成員 private

圖片說明:

 

p200882212174

參考:

http://msdn.microsoft.com/zh-tw/library/ms173121(v=vs.100).aspx

http://www.cnblogs.com/weihai2003/archive/2008/10/24/1319003.html

http://www.wretch.cc/blog/kaezorr/4761263

Read more...

Web From 擷圖

2013年3月14日 星期四

今天主管要評估一個需求,那就是可不可以做跟PChome一樣,可以把網頁畫面擷取下來存成圖檔 於是就展開了一段漫長的尋找,雖然網路上也一些api可以用,但我還是找到方法 原理:先載入要擷圖的url,在利用window.from的WebBrowser元件來將網頁轉成圖片 來看一下code 吧

protected void btn1_Click(object sender, EventArgs e)
{
string url = "http://tw.yahoo.com";
Bitmap bitmap = new Bitmap(CaptureWebPage(url));
Response.ContentType = "image/jpeg";
bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
bitmap.Dispose();
Response.End();
}

public System.Drawing.Bitmap CaptureWebPage(string URL)
{
// create a hidden web browser, which will navigate to the page
System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser();
// we don't want scrollbars on our image
web.ScrollBarsEnabled = false;
// don't let any errors shine through
web.ScriptErrorsSuppressed = true;
// let's load up that page!
web.Navigate(URL);

// wait until the page is fully loaded
while (web.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(1500); // allow time for page scripts to update
// the appearance of the page

// set the size of our web browser to be the same size as the page
int width = web.Document.Body.ScrollRectangle.Width;
int height = web.Document.Body.ScrollRectangle.Height;
web.Width = width;
web.Height = height;
// a bitmap that we will draw to
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
// draw the web browser to the bitmap
web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));

return bmp; // return the bitmap for processing
}


執行後會發生"無法產生 ActiveX 控制項 '8856f961-340a-11d0-a96b-00c04fd705a2',因為目前的執行緒不是在單一執行緒 Apartment。" 的問題 “



解決方法,可以至.aspx 加入 AspCompat=”ture



<%@ Page Title="首頁" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default"  AspCompat="true"%>

Read more...

避免巢狀判斷式

2013年3月8日 星期五

通常我們在學程式設計時,會用到if else 

寫久了就會習慣這樣寫

int period = int.Parse(installment);
if(period>1)
{
var list = getList();
if(list.count()>1)
{
//資料處理
}

}


這樣寫是沒有問題的,但是在閱讀上會比較沒這麼清楚



 



那可以怎麼改呢?接我們繼續看下去…



int period = 0;
if(!int.TryParse(installment, out period))
{
continue;
}

if(period<=1)
{
continue;
}


//資料處理


做法修改為上面先做驗證,下方才進行資料處理動作,這樣看是不是就清楚多了呢?

Read more...

Javascript 存取物件屬性的方式

2013年2月28日 星期四

在取動態物件時會需要用到

JavaScript 有兩種方式可以用來存取物件屬性,假設我們有以下這個物件:

var myObject = {
att1: '123',
att2: '456',
func1: function () {},
func2: function () {}
}


第一種存取的方法是用 JavaScript 的物件屬性存取子「句點 (.) 」:



myObject.attr1;
myObject.attr2;
myObject.func1();
myObject.func2();

第二種方法是利用關聯索引來存取:

myObject['attr1'];
myObject['attr2'];
myObject['func1']();
myObject['func2']();

為什麼要有第二種方法呢?這就是今天的主題了,如果今天我要動態存取物件屬性要怎麼做呢?




動態存取



找出myObject 物件裡的各項屬性,可以用以下的方法



for (var attrName in myObject) {
alert(attrName + ':' + 每個屬性的值);
}


這裡 attrName 會依序是 attr1, attr2, func1, func2 ,那麼我們怎麼抓到每個屬性的值呢?



那就是用第二種方法:



for (var attrName in myObject) {
alert(attrName + ':' + myObject[attrName]);
}


在這裡 attrName 會自動轉型成字串,用來當做關聯索引的值,也讓我們能夠存取到 myObject 物件裡對應的資訊。



那為什麼不能用第一種方法呢?



for (var attrName in myObject) {
alert(attrName + ':' + myObject.attrName); // myObject.attrName 可以嗎?
}

因為 JavaScript 規定在句點後面不能接上一個變數,所以如果我們用 myObject.attrName 的話,會被 JavaScript 解釋成要存取 myObject 的 attrName 屬性。

那可以用eval嗎?



for (var attrName in myObject) {
alert(attrName + ':' + eval('myObject.' + attrName));
}

儘可能別用eval,因為他會拖慢速度,所以還是用第二種方式就好啦。

Read more...

$(window).scrollTop() vs. $(document).scrollTop()

2013年2月19日 星期二

筆記一下

They are both going to have the same effect.

Both scroll to the top of the object, an html document is considered the "document" thus scrolling to the top of the html page. A "window" object is created with each frame, thus your main window is one frame, if you had an "iframe" that would create another window object. So your just scrolling to the top of the window object, creating the same effect.

It seems that the window object is buggy when it comes to this. It apprently does not work in IE8 and Opera, so it is best to use $("html").scrollTop().

所以最好是用$("html").scrollTop().可支援多瀏覽器

 

 

 

 

資料來源:http://stackoverflow.com/questions/5371139/window-scrolltop-vs-document-scrolltop

Read more...