2011年10月25日

[ASP.NET]Response與Postback

目前開發中的專案,有一項功能是使用者能針對資料進行申請下載,當使用者點選申請列表的下載按鈕或整批下載按鈕時,資料將匯出成EXCEL檔案,並供使用者作後續使用,而位於申請下載列表的資料則轉入已下載列表內,如下圖所示。

  • 下載EXCEL檔案的程式碼如下:
  • Response.Clear();                
    Response.WriteFile(physicPath + fileName);
    string httpHeader = "attachment;filename=backup.Xls";
    Response.ContentType = "application/vnd.ms-excel";
    Response.AppendHeader("Content-Disposition", httpHeader);
    Response.Flush();
    Response.End();
    
實際執行功能時,系統順利的顯示檔案下載視窗,但卻無法將申請下載列表內的資料,轉入已下載列表,搜尋網路上的資料,原因為:
當使用到 Response.End 方法時,會呼叫內部的Thread.Abort()方法,引發ThreadAbortException 的例外,導致網頁的執行停止,並略過 Response.End 以下的程式碼。詳細說明,可參考Response.End() Exceptions in ASP.NET
本案例的情況為,網頁雖有作postback,但遇到Response的相關操作,程式執行過程中的部分程序將會被略過或終止,轉為將檔案輸出至用戶端,使得網頁並未回傳至用戶端的瀏覽器。
  • 解決方式,大多建議開新視窗進行檔案下載,開新視窗作檔案下載之程式碼如下:
  • string newWin = "window.open('" + "WEBFileURL" + fileName + "');";
    ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);

2011年9月23日

IE8相容性模式問題

近期一個專案是以Visual Studio 2010開發ASP.NET網站,在套用版面及CSS樣式過程中,皆直接使用Visual Studio的[在瀏覽器檢視功能],以IE8檢視版面並進行修正,經過一番折騰,內容顯示終於跟設計稿相去不遠,但將網站佈署至IIS後,再以IE8檢視時,XD,怎麼有種七月半的感覺(版面破相了XD),請教Google神,獲得的解答是IE8相容性模式問題。
若HTML網頁沒有定義DOCTYPE要以何種模式作呈現,將會採用Quirks模式。
Visual Studio的[在瀏覽器檢視功能]似乎是以IE8標準模式作呈現,故將網頁定義為IE8標準模式,即可解決該問題。
  • 作法可參考以下網址的說明:
    • http://tsuozoe.pixnet.net/blog/post/24804436-internet-explorer-8
    • http://msdn.microsoft.com/zh-tw/library/cc817570.aspx
  • 也可透過程式碼,進行網頁定義:
  • protected override void OnPreInit(EventArgs e)
    {
        Response.AddHeader("X-UA-Compatible", "IE=8");
        base.OnPreInit(e);
    }
    

2011年9月21日

Update Panel Scrolling 問題

  • 一般以ASP.NET開發的網頁,若需在postback後,仍保持原本scrolling的位置,可於Page標籤加入MaintainScrollPositionOnPostback="true"屬性,即可解決。
  • <%@ Page Language="C#" AutoEventWireup="true" CodeFile="FindLocation.aspx.cs" Inherits="FindLocation" MaintainScrollPositionOnPostback="true" %>
    
  • 但若Scrolling的容器(ex:div)位於Update Panel內,則上述設定將無法讓Scrolling保持原有位置,解決方法如下:
  • 於ScriptManager標籤後加入以下JavaScript程式碼
      <asp:ScriptManager ID="ScriptManager" runat="server">
        </asp:ScriptManager>
        <script type="text/javascript">
            var xPos, yPos;
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_beginRequest(BeginRequestHandler);
            prm.add_endRequest(EndRequestHandler);
            function BeginRequestHandler(sender, args) {
                xPos = $get('gvDiv').scrollLeft; //gvDiv請更換成適合的ID名稱
                yPos = $get('gvDiv').scrollTop;
            }
            function EndRequestHandler(sender, args) {        
                $get('gvDiv').scrollLeft = xPos;
                $get('gvDiv').scrollTop = yPos;
            }
         </script>
    
參考來源:http://forums.asp.net/t/1156877.aspx/2/10

FLEX嵌入HTML時,HTML元素無法輸入中文問題

  • 解決方法:
  • 在指定為Application的mxml頁面,加入監聽MouseEvent.ROLL_OUT事件,相關程式碼如下:
    
    vc.addEventListener(MouseEvent.ROLL_OUT, setSRF); //vc請自行修改成合適的ID名稱
    
    private function setSRF(event:MouseEvent):void{ 
         if(Capabilities.hasIME){ 
              IME.enabled=true; 
              IME.conversionMode="CHINESE"; 
         } 
    }
    

參考來源:http://blog.csdn.net/vipliyaohua/article/details/6586457

2011年3月23日

Factory Method Pattern(工廠模式)

  • Definition
  • The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
    
  • Class Diagram
  • Product Role
  • public abstract class Product {
        public void method1() {
        //doSomething
        }
    
    public abstract void method2() ;
    
    }
    
  • ConcreteProduct Role
  • public ConcreteProduct1 extend Product {
        public void method2() {
            //doSomething
        }
    }
    
    public ConcreteProduct2 extend Product {
        public void method2() {
            //doSomething
        }
    }
    
  • Creator Role
  • public abstract class Creator {
        public abstract <T extends Product> T createProduct(Class<T> c);
    }
    
  • ConcreteCreator Role
  • public class ConcreteCreator extends Creator {
        public <T extends Product> T createProduct(Class<T> c) {
            Product product=null;
            try {
                product = (Product)Class.forName(c.getName()).newInstance();
            } catch (Exception e) {
                //error process
            }
            Return (T)product;
        }
    }
    
  • Client Role
  • public class Client {
        public static void main(String[] args) {
            Creator creator = new ConcreteCreator();
            Product product = creator.creatorProduct(ConcreteProduct1.class);
            //doSomething
        }
    }
    

2011年3月2日

Decorator Pattern(裝飾模式)

  • Definition
  • The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
    
  • Class Diagram
  • Component Role
  • public abstract class Component {
        public abstract void operate();
    }
    
  • ConcreteComponent Role
  • public class ConcreteComponent extends Component {
        @Override
        public void operate() {
            System.out.println("do Something");
        }
    }
    
  • Decorator Role
  • 
    public abstract class Decorator extends Component {
        private Component _component;
        
        public Decorator(Component component) {
            this._component = component;
        }
    
        @Override
        public void operate() {
            this._component.operate();
        }
    }
    
  • ConcreteDecorator Role
  • public class ConcreteDecorator1 extends Decorator {
    
        public ConcreteDecorator1(Component component)
            super(component);
        }
    
        private void method1() {
            System.out.println("method1 Decorator");
        }
    
        public void operate() {    
            this.method1();
            super.operate();
        }
    }
    
    public class ConcreteDecorator2 extends Decorator {
    
        public ConcreteDecorator2(Component component)
            super(component);
        }
    
        private void method2() {
            System.out.println("method2 Decorator");
        }
    
        public void operate() {    
            this.method2();
            super.operate();
        }
    }
    
  • Client Role
  • public class Client {
        public static void main(String[] args) {
            Component component = new ConcreteComponent();
            component = new ConcreteDecorator1(component);
             component = new ConcreteDecorator2(component);
            component.operate();
        }
    }
    

2011年2月28日

Observer Pattern(觀察者模式)

  • Definition
  • The Observer Pattern defines a one-to-many dependency between 
    objects so that when one object changes state, all of its 
    dependents are notified and updated automatically.
    
  • Class Diagram
  • Subject Role
  • public abstract class Subject {
        private Vector<Observer> obsVector = new Vector<Observer>();
        
        public void addObserver(Observer o) {
            this.obsVector.add(o);
        }
        
        public void delObserverr(Observer o) {
            this.obsVector.remove(o);
        }
    
        public void notifyObservers() {
            for (Observer o:this.obsVector) {
                o.update();
            }
        }
    }
    
  • ConcreteSubject Role
  • public class ConcreteSubject extends Subject {
        public void doSomething() {
             //do something
            notifyObserver();
        }
    }
    
  • Observer Role
  • public interface Observer {
        public void update();
    }
    
  • ConcreteObserver Role
  • public class ConcreteObserver implements Observer {
        pulic void update() {
            system.put.println("received notification, do somthing");
        }
    }
    
  • Client Role
  • public class Client {
        public static void main(String[] args) {
            ConcreteSubject subject = new ConcreteSubject();
            Observer obs = new ConcreteObserver();
            subject.addObserver(obs);
            subject.doSomething(); 
        }
    }
    

2011年2月27日

Strategy Pattern(策略模式)

  • Definition
  • The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
    
  • Class Diagram
  • Strategy Role
  • public interface Strategy {
        public void doSomething();
    }
    
  • ConcreteStrategy Role
  • public class ConcreteStrategy1 implements Strategy {
        public void doSomething(){
            System.out.println(“ConcreteStrategy1’s algorithm”);
        }
    }
    public class ConcreteStrategy2 implements Strategy {
        public void doSomething(){
            System.out.println(“ConcreteStrategy2’s algorithm”);
        }
    }
    
  • Context Role
  • public class Context {
        private Strategy _strategy;
        
        public Context(Strategy strategy){
            this._strategy= strategy;
        }
    
        public performDoSomething(){
            this._strategy.doSomething();
        }
    }
    
  • Client Role
  • public class Client {
        Strategy strategy = new ConcreteStrategy1();
        Context context = new Context(strategy);
        context.performDoSomething();
    }
    

2011年2月22日

[Python] Handle many levels of nested lists

list於Python中,是有序的物件集合,list內的元素若是list,則形成巢狀結構,若要對巢狀list內的每項元素作相同的處理或運算,則可以遞迴方式作回應。
  • Ex:顯示每項元素於螢幕上(程式碼)
  • def print_item(the_list):
        for each_item in the_list:
            if isinstance(each_item, list):
                print_item(each_item)
            else:
                print(each_item)
    
  • 執行結果顯示

2011年1月17日

Ubuntu 10.04 wubi無法啟動問題

在近一次的自動更新後,於開機選單就無法選擇Ubuntu啟動,錯誤訊息如下:
Try (hd0,0): NTFS5: No wubildr

error: unknown command 'loadfont'
error
求助Google大神得到的解決方式如下:
  • 以Ubuntu Live CD或Ubuntu Live USB開機。
  • 於Terminal輸入以下指令: 
    • 注意:sda1是指windows作業系統所在磁區。
sudo mkdir /media/win 
sudo mount /dev/sda1 /media/win
sudo mount -o loop /media/win/ubuntu/disks/root.disk /mnt
sudo cp /mnt/boot/grub/grub.cfg /mnt/boot/grub/grub.cfg.copy
sudo chmod +w /mnt/boot/grub/grub.cfg
gksu gedit /mnt/boot/grub/grub.cfg
  • 修改grub.cfg檔 
    • 筆者是將以下程式碼刪除,再重新開機,即可登入Ubuntu作業系統。
menuentry "Ubuntu, Linux 2.6.32-27-generic" {
    insmod ntfs
    set root='(hd0,2)'
    search --no-floppy --fs-uuid --set 74a62494a62458be
    loopback loop0 /ubuntu/disks/root.disk
    set root=(loop0)
    linux /boot/vmlinuz-2.6.32-27-generic root=/dev/sda2 loop=/ubuntu/disks/root.disk ro   quiet splash
    initrd /boot/initrd.img-2.6.32-27-generic
}
menuentry "Ubuntu, Linux 2.6.32-27-generic (recovery mode)" {
    insmod ntfs
    set root='(hd0,2)'
    search --no-floppy --fs-uuid --set 74a62494a62458be
    loopback loop0 /ubuntu/disks/root.disk
    set root=(loop0)
    linux /boot/vmlinuz-2.6.32-27-generic root=/dev/sda2 loop=/ubuntu/disks/root.disk ro single 
    initrd /boot/initrd.img-2.6.32-27-generic
}
menuentry "Ubuntu, Linux 2.6.32-26-generic" {
    insmod ntfs
    set root='(hd0,2)'
    search --no-floppy --fs-uuid --set 74a62494a62458be
    loopback loop0 /ubuntu/disks/root.disk
    set root=(loop0)
    linux /boot/vmlinuz-2.6.32-26-generic root=/dev/sda2 loop=/ubuntu/disks/root.disk ro   quiet splash
    initrd /boot/initrd.img-2.6.32-26-generic
}
menuentry "Ubuntu, Linux 2.6.32-26-generic (recovery mode)" {
    insmod ntfs
    set root='(hd0,2)'
    search --no-floppy --fs-uuid --set 74a62494a62458be
    loopback loop0 /ubuntu/disks/root.disk
    set root=(loop0)
    linux /boot/vmlinuz-2.6.32-26-generic root=/dev/sda2 loop=/ubuntu/disks/root.disk ro single 
    initrd /boot/initrd.img-2.6.32-26-generic
}

解決方案參考網址