2014年11月13日 星期四

ASP.NET WebForm + JQuery RadioButtonList變更時,啟用DropDownList控制項

本篇直接使用JQuery來啟用或停用DropDownList,重點不使用ASP.NET的AJAX功能

前置作業:請先到JQuery官方網站下載jquery(我是使用v1.8.2)

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="./Scripts/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //小於%=RBL_bhours.UniqueID %大於 是ASP的寫法,會將RBL_bhours控制項的唯一識別印出(類似ID)
            $('input[type="radio"][name="<%=RBL_bhours.UniqueID %>"]').change(function () {
                var bhours = this.value;
                //會先找到id="div1" 再 循序處理每個 select元素
                $('#div1 select').each(function () {
                    if (bhours == 'true') {
                        $(this).removeAttr('disabled'); //移除disabled等於啟用select元素
                    } else {
                        $(this).attr('disabled', 'disabled'); //加入disabled等於停用select元素
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:RadioButtonList ID="RBL_bhours" runat="server"
            RepeatDirection="Horizontal" RepeatLayout="Flow">
            <asp:ListItem Value="true">開啟</asp:ListItem>
            <asp:ListItem Value="false" Selected="True">關閉</asp:ListItem>
        </asp:RadioButtonList>
        &nbsp;&nbsp;
        <div id="div1">
            每月
            <asp:DropDownList ID="DDL_ts" runat="server" Width="45px" Enabled="false">
                <asp:ListItem>01</asp:ListItem>
                <asp:ListItem>02</asp:ListItem>
                <asp:ListItem>03</asp:ListItem>
            </asp:DropDownList>日
            到
            每月
            <asp:DropDownList ID="DDL_te" runat="server" Width="45px" Enabled="false">
                <asp:ListItem>01</asp:ListItem>
                <asp:ListItem>02</asp:ListItem>
                <asp:ListItem>03</asp:ListItem>
            </asp:DropDownList>日
        </div>
    </form>
</body>
</html>

2014年11月12日 星期三

javascript 應用篇




2017/06/16
取代字串中所有的blue為red。
var str = "Mr Blue has a blue house and a blue car".replace(/blue/g, "red");
取代字串中所有的blue為red,不區分大小寫。
var str = "Mr Blue has a blue house and a blue car".replace(/blue/gi, "red");
(參考:JavaScript String replace() Method)

2017/06/13
字串轉數值時,須注意字串前面是不是多了個0,當字串的數字是08、09時,parseInt會把它當作八進制處理,但是08、09都是不正確的八進制,我們可以在parseInt再加上第二個參數,例如:parseInt("08",10),告訴parseInt按照十進制處理,這樣就沒有問題了。
(參考:關于javascript中parseInt函數的一個所謂的bug)

2015/07/17
1. 全選checkbox。
<Script type="text/javascript">
function stcb(objcb){
  var checkboxs = document.getElementsByName('CB_Del'); 
  for(var x=0;x<checkboxs.length;x++){
    checkboxs[x].checked = objcb.checked;
  };
};
</Script>
<input type="checkbox" name="cb_total" id="cb1" onclick="stcb(this);" />
<label for="cb1" style=" font-size:10px;">全選</label>
<input type="checkbox" name="CB_Del" value="1">
<input type="checkbox" name="CB_Del" value="2">
<input type="checkbox" name="CB_Del" value="3">
<input type="checkbox" name="CB_Del" value="4">
<input type="checkbox" name="CB_Del" value="5">

2.變更form的action,並送出。
<Script type="text/javascript">
function but1_click(){
  document.f1.action='test.asp';
  document.f1.submit();
};
</Script>
....
<form name="f1" method="post">
<input name="but1" type="button" value="按鈕" onclick="but1_click();">
</form>
....

2015/05/20
1.變更link標籤的css檔
Step 1.首先再head標籤中引入css,id為cf1,如下:
<head>
...
<link rel="stylesheet" href="" id="cf1" />
...
</head>
Step 2.寫入下方的JavaScript,並呼叫它,即可修改CSS。
function change_css(){
  var css = document.getElementById("cf1");
  css.href = 'stylesheets/css1.css';
};

2.兩個日期時間相減,經過多少分鐘(取得經過多少毫秒轉分鐘)。
var d1=new Date("2015/05/19 19:18"); //假設登入時間
//getTime() 取得時間 (由 1970-01-01 00:00 到目前時間) 單位:(毫秒)
document.write('d1='+d1+' d1_num='+d1.getTime()+'<br/>');
var d2=new Date("2015/05/20 19:18"); //假設現在時間
document.write('d2='+d2+' d2_num='+d2.getTime()+'<br/>');
var num=d2.getTime()-d1.getTime(); //經過多少毫秒=目前時間(毫秒)-登入時間(毫秒)
document.write('num='+num+'<br/>');
//若要將毫秒數轉換為天數,請用 86,400,000 (1000 毫秒 x 60 秒 x 60 分鐘 x 24 小時) 除以毫秒數
document.write('毫秒 轉 分鐘='+(num/(1000*60))+'<br/>');

2015/03/30
1.按下按鈕出現請稍候訊息(請自行將ButtonFinish方法寫在Button的onclick事件中)
    <script type="text/javascript">
        var strpoint = '';
        function rec() {
            if (strpoint != '') {
                if (strpoint.length <= 2) {
                    strpoint = strpoint + '.';
                } else {
                    strpoint = '';
                };
            } else {
                strpoint = '.';
            };
            $('#span1').html('訊息:請稍候' + strpoint);
            setTimeout("rec();", 1000);
        };
        function ButtonFinish() {
            setTimeout(function () { rec(); }, 500);
        };
    </script>

2014/11/13
1.判斷手機是否為瀏覽器或手機瀏覽
    function checkerAgent() {
        var flag = false;
        if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
            flag = true;
        } else {
            //this is the browser
            flag = false;
        };
        if (flag == true) {
            document.location.href = './index1.html';
        } else {
            document.location.href = './index2.html';
        };
    };

2.倒數5秒鐘
    var s1=5;
    function rec() {
        if (s1 != 0) {
            $('#span1').text('倒數' + s1 + '秒'); //使用jquery設定span標籤
            setTimeout("rec();", 1000);
            s1 = s1 - 1;
        } else {
             $('#span1').text('時間到!!');
        };
    };

2014年11月10日 星期一

javascript 驗證表單格式


利用JQuery 取得欄位值,在透過javascript驗證檢查資料格式是否正確
function check_form(conphone, email) {
var str_message='';
var reg, strtel, strEmail;
strtel = ($('#' + conphone).val()).trim(); strEmail = ($('#' + email).val()).trim();
reg =/^[A-Z]\d{9}$/;  //身份證字號
reg = /(09[0-9]{2}-[0-9]{3}-[0-9]{3})|([0-9]{2}-[0-9]{6,8})$/;
if (reg.test(strtel) == false) {
str_message = f1(str_message, '聯絡電話格式錯誤,範例:0912-111-111或06-7211111');
};
reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]+$/;
if (reg.test(strEmail) == false) {
str_message = f1(str_message, 'E-mail格式錯誤,範例:test@gmail.com');
};

if (str_message != '') {
alert(str_message);
return false;
};
return confirm("確定要送出嗎?");
};

function f1(str_mess, str_add) {
    if (str_mess == '') { str_mess = str_add + '\n'; } else { str_mess = str_mess + str_add + '\n'; };
    return str_mess;
};

2014年11月6日 星期四

ASP(Active Server Pages) 應用技巧篇


2014/11/07
1.取得host,並組成網址
<%
        '取得host,並檢查是否是https
if Request.ServerVariables("HTTPS")="on" then
str_url="https://" & Request.ServerVariables("HTTP_HOST")
else 'off
str_url="http://" & Request.ServerVariables("HTTP_HOST")
end if
Response.write(str_url)

        '========================
        '印出Request.ServerVariables所有資料
'for each x in Request.ServerVariables
'    response.write(x & "=" & Request.ServerVariables(x) & "<br>")
'next
%>

2014年10月17日 星期五

ASP 使用instr及Mid函數,從字串中擷取某一段字串


<%
dim str_temp1,SEHM_s,SEHM_e,str_SEHM,new_temp1

'假設str_temp1是我自己儲存在資料庫裡的資料,而我要擷取的是SEHM=10:00~12:00這段資料
str_temp1="TEMP=ABCD;SEHM=10:00~12:00;TEMP2=EFGH;"

'搜尋SEHM在字串中的起始位置
SEHM_s=instr(str_temp1,"SEHM")  '搜尋出來的位置是11

'instr(參數1 ,參數2 ,參數3)
'參數1:從哪一個位置,開始搜尋
'參數2: 此處指的是str_temp1
'參數3:要搜尋的字串
SEHM_e=instr(SEHM_s,str_temp1,";")+1  '搜尋出來的位置是27再加1等於28,加1是為了取得分號(;)

'Mid擷取字串的函數; Mid(str_temp1, 起始位置 ,(SEHM_e-SEHM_s))
str_SEHM=Mid(str_temp1,SEHM_s,(SEHM_e-SEHM_s))
Response.write("取得SEHM:" & str_SEHM & "<br/>")

new_SEHM="SEHM=09:00~10:00;" '假設是變更後的資料
'============================
'法1.直接取代原本的字串
new_temp1=Replace(str_temp1,str_SEHM,new_SEHM)
'============================
'法2.先將原本找到的字串資料替換成空白,在最後面插入新的資料
'new_temp1=Replace(str_temp1,str_SEHM,"")
'new_temp1=new_temp1 & new_SEHM
response.write(new_temp1 & "<br/>")
%>

2014年7月5日 星期六

ASP HtmlDecode 字串編碼轉換html

參考資料1:ASP Encode/Decode Functions

說明:當資料庫的資料儲存了換行的編碼,如:「 &#38;lt;br /&#38;gt;」(這是一個html換行的編碼)。那該如何轉換成html <br/>呢? 請看下列程式碼

程式碼
<%
Function HTMLDecode(sText)
    Dim I
    sText = Replace(sText, "&quot;", Chr(34))
    sText = Replace(sText, "&lt;"  , Chr(60)) '&lt 會被取代為 < 符號
    sText = Replace(sText, "&gt;"  , Chr(62)) '&gt 會被取代為 > 符號
    sText = Replace(sText, "&amp;" , Chr(38))
    sText = Replace(sText, "&nbsp;", Chr(32))
    For I = 1 to 255
        sText = Replace(sText, "&#" & I & ";", Chr(I)) '&#38 會被取代為 & 符號
    Next
    HTMLDecode = sText
End Function

Dim str_text
str_text="第一點&#38;lt;br /&#38;gt;第二點" '假設此變數為資料庫取出的資料
str_text=HTMLDecode(str_text) '呼叫第一次,此時回傳回來的值為「第一點&lt;br /&gt;第二點」
str_text=HTMLDecode(str_text) '呼叫第二次,此時回傳回來的值為「第一點<br />第二點」

Response.write str_text
%>

P.S 在傳統的ASP中沒有Server.HtmlDecode方法所以必須自行加入 HTMLDecode方法

2014年6月27日 星期五

ASP 使用JMail寄信

參考資料:http://www.ravs.ntct.edu.tw/know/show.asp?QUESTIONID=50

前置作業:
安裝JMail
1-1.先到Dimac Development下載 w3Jmail,下載前他會要您填寫資料,填寫完後即可下載,下載完後請先行安裝。
1-2.安裝完後,請到安裝的路徑(如 C:\Program Files (x86)\Dimac\w3JMail\ )底下複製 jmail.dll,請注意這裡將會有兩種狀況。狀況1:如果您的電腦是64bit請將檔案貼到C:\Windows\SysWOW64 ;狀況2:如果是32bit請將檔案貼到C:\Windows\System32。
1-3.請使用命令提示字元,下指令安裝JMail。
    (1)如果是32bit,請在命令提示字元執行regsvr32 jmail.dll
    (2)如果是64bit,請看下圖




程式碼:
Function JMailSend(MailTo,MailSubject,str_body,mailacc,mailpass,mailurl,mailcomp)
    Dim HTMLMailBody
    On Error Resume Next 
set jmail= server.CreateObject ("jmail.message")
jmail.Silent = true
jmail.Charset = "utf-8"

 '附加檔案
'filefullpath = Server.Mappath(".") & "\" & "test.doc"
'JMail.AddAttachment(filefullpath)

HTMLMailBody="<html><head><meta content=" & chr(34) & "text/html;" & chr(34) & " charset=" & chr(34) & "utf-8" & chr(34) & " http-equiv=" & chr(34) & "Content-Type" & chr(34) & "><title>test</title><style type=" & chr(34) & "text/css" & chr(34) & ">" & "</style></head><body>" & str_body &"</body></html>"

jmail.From = mailcomp '寄信人位址
jmail.FromName = "台灣" '寄件人名稱
jmail.ReplyTo = mailcomp '回信位址
jmail.Subject = MailSubject '信件title
jmail.AddRecipient MailTo '收件人地址
jmail.Body = "我們的郵件採用了HTML格式"
JMail.HTMLBody = HTMLMailBody


jmail.MailServerUserName = mailacc ' SMTP 登入帳號
jmail.MailServerPassWord = mailpass ' SMTP 登入密碼
jmail.Send(mailurl) '指定送信伺服器 SMTP
jmail.Close
set jmail = nothing
if Err.Number<>0 then '錯誤處理
'response.write Err.Description 
JMailSend=1
else 
 JMailSend=0
end if 


End Function