2014年5月16日 星期五

弄堂

第一次是在相聲瓦舍的相聲劇大作-鄧力軍裡聽到
該劇全部的音樂歌曲皆由張穆庭製作
這首是片尾曲
Youtube上可以搜出好幾個結果,但我最喜歡這個
當然是因為洪瑞襄的緣故

繁華散盡 落葉飄
徒然一身幽居嘆寂寥
迎風身影燈火照 但求醉臥在今宵
不知何時才能 與你再相逢 也許是多年以後
此情忘不了 深藏在心中 但願你會記得我
如果不能與你長相守 只願我能化成飛煙隨你走
就在夢中一笑別離後 淚珠滑落臉龐 更心痛
如果不能與你長相守 只願我能化成飛煙隨你走
多年以後往事再回首 笑我一生孤獨 全成空

2014年5月10日 星期六

多組RadioButtonList互斥

一個同事的發問...

他在畫面上放了 3 組 RadioButtonList,希望其中一組選到時,另兩組可以自動清空選擇,於是我寫了這個範例

基本上兩種做法,偷懶的做法是放個 UpdatePanel,把 RadioButtonList 們都扔進去,比較好的做法是用 javascript 在 client 上處理。

首先是偷懶的做法
這是 aspx,有 3 個 RadioButtonList,都設定 SelectedIndexChanged 事件和設定 AutoPostBack=true
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:RadioButtonList ID="rbl1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"
                OnSelectedIndexChanged="rbl1_SelectedIndexChanged" AutoPostBack="true">
                <asp:ListItem>Car</asp:ListItem>
                <asp:ListItem>Motocycle</asp:ListItem>
                <asp:ListItem>Bicycle</asp:ListItem>
            </asp:RadioButtonList>
            <br />
            <br />
            <asp:RadioButtonList ID="rbl2" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"
                OnSelectedIndexChanged="rbl2_SelectedIndexChanged" AutoPostBack="true">
                <asp:ListItem>Apple</asp:ListItem>
                <asp:ListItem>Mango</asp:ListItem>
                <asp:ListItem>Pineapple</asp:ListItem>
            </asp:RadioButtonList>
            <br />
            <br />
            <asp:RadioButtonList ID="rbl3" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"
                OnSelectedIndexChanged="rbl3_SelectedIndexChanged" AutoPostBack="true">
                <asp:ListItem>Frog</asp:ListItem>
                <asp:ListItem>Cat</asp:ListItem>
                <asp:ListItem>Dog</asp:ListItem>
            </asp:RadioButtonList>
        </ContentTemplate>
    </asp:UpdatePanel>
</form>
然後這是 aspx.cs
protected void rbl1_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonList radioButtonList = sender as RadioButtonList;
    if (radioButtonList.SelectedIndex > -1)
    {
        rbl2.SelectedIndex = -1;
        rbl3.SelectedIndex = -1;
    }
}

protected void rbl2_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonList radioButtonList = sender as RadioButtonList;
    if (radioButtonList.SelectedIndex > -1)
    {
        rbl1.SelectedIndex = -1;
        rbl3.SelectedIndex = -1;
    }
}

protected void rbl3_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonList radioButtonList = sender as RadioButtonList;
    if (radioButtonList.SelectedIndex > -1)
    {
        rbl2.SelectedIndex = -1;
        rbl1.SelectedIndex = -1;
    }
}

接著我們用 jquery 來做
一樣在 aspx 上放 3 個 RadioButtonList,然後把所有的 radio 都註冊 click 事件,當一個 radio 被 checked 時,往上找該 radio 的容器,然後把另兩個容器裡的 radio 都 uncheck
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestRadioButtonListExclusive.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>用 jquery</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
        //註冊所有 radio 的 click 事件
        $(document).ready(function(){
            var containers=["<%=rbl1.ClientID %>","<%=rbl2.ClientID %>","<%=rbl3.ClientID %>"];

            for(var i=0;i<containers.length;i++){
                $("#"+containers[i]+" :radio").each(function(){
                    $(this).click(function(event){
                        SetOtherRadioButtonListUncheck(this);
                    });
                });
            }
        });
        
        function SetOtherRadioButtonListUncheck(rObj){
            var containers=["<%=rbl1.ClientID %>","<%=rbl2.ClientID %>","<%=rbl3.ClientID %>"];
            
            //抓上層容器, RepeatLayout=Flow的話抓最近的span, RepeatLayout=Flow的話抓最近的table
            //var parentObj=$(rObj).closest("span"); //RepeatLayout=Flow
            var parentObj=$(rObj).closest("table"); //RepeatLayout=Table
            var parentId=parentObj.get(0).id;
            
            for(var i=0;i<containers.length;i++){
                //若不是觸發事件的容器, 則將該容器裡的radio uncheck
                if(containers[i]!=parentId){
                    $("#"+containers[i]).find(":radio").each(function(){
                        this.checked=false;
                    });
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:RadioButtonList ID="rbl1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Table">
            <asp:ListItem>Car</asp:ListItem>
            <asp:ListItem>Motocycle</asp:ListItem>
            <asp:ListItem>Bicycle</asp:ListItem>
        </asp:RadioButtonList>
        <br />
        <br />
        <asp:RadioButtonList ID="rbl2" runat="server" RepeatDirection="Horizontal" RepeatLayout="Table">
            <asp:ListItem>Apple</asp:ListItem>
            <asp:ListItem>Mango</asp:ListItem>
            <asp:ListItem>Pineapple</asp:ListItem>
        </asp:RadioButtonList>
        <br />
        <br />
        <asp:RadioButtonList ID="rbl3" runat="server" RepeatDirection="Horizontal" RepeatLayout="Table">
            <asp:ListItem>Frog</asp:ListItem>
            <asp:ListItem>Cat</asp:ListItem>
            <asp:ListItem>Dog</asp:ListItem>
        </asp:RadioButtonList>
    </form>
</body>
</html>

2014年4月29日 星期二

使用匿名方法賦值給匿名類別的屬性

好繞口... 

以北風資料庫的Products table 為例,假設我想做一個某家供應商的產品List,並把價格提高10%,於是我這麼寫...
var v = from product in dtNorthwindProduct.AsEnumerable()
        where product.Field<int>("SupplierID") == 1
        select new
        {
            pid = product.Field<int>("ProductID"),
            //以匿名方法重新計算價格
            price = () =>{ return product.Field<decimal>("UnitPrice") * 1.1M }
        };
.NET很乾脆地給我錯誤...

因為初始匿名型別的屬性有兩個限制
  1. 不能是null
  2. 不能用匿名方法賦值
所以要這麼寫...
var v = from product in dtNorthwindProduct.AsEnumerable()
        where product.Field<int>("SupplierID") == 1
        select new
        {
            pid = product.Field<int>("ProductID"),
            //以匿名方法重新計算價格
            price = new Func<decimal>(() => { return product.Field<decimal>("UnitPrice") * 1.1M; }).Invoke()
        };
就可以了...

參考連結
C#/.NET Little Wonders: The Joy of Anonymous Types
Assigning property of anonymous type via anonymous method

2014年4月24日 星期四

以 regular expression 驗證 Email 格式

筆記用...

檢查字串
/^.+@.+\..+$/

C#
public bool CheckEmailFormat(string email)
{
    //c#裡要把前後的斜線拿掉
    string emailFormat = @"^.+@.+\..+$";
    System.Text.Reqex reg = new System.Text.Regex(emailFormat);
    return reg.IsMatch(email);
}

Javascript
function IsEmail(value) {
    var objRegExp = /^.+@.+\..+$/;

    if (!objRegExp.test(value))
        return false;
    return true;
}

2014年3月17日 星期一

Supercell-ヒーロー


2014年3月12日 星期三

Supercell-君の知らない物語


每回聽這首歌就會想到戰場原的告白,然後眼睛就流汗了啊!!

2014年3月6日 星期四

使用ODP.NET連線出現 "Connection must be open for this operation" 錯誤

但其實不是connection沒開的問題,ODP.NET根本在整人...