2014年10月28日 星期二

強制下載檔案

現在瀏覽器日新月異,像PDF檔之類的

按下去不像以前是下載,而是開新頁幫你打開檔案

而下面這個方面是讓檔案直接下載

先開一個新的頁面,把下載路徑導到那頁

下面為程式碼

//設定要下載的檔案路徑  
string path = 下載路徑;
strFileName = 檔名 + 副檔名;
//宣告並建立WebClient物件
WebClient wc = new WebClient();

//載入要下載的檔案
byte[] b = wc.DownloadData(Server.MapPath(path));

//清除Response內的HTML
Response.Clear();

//設定標頭檔資訊 attachment 是本文章的關鍵字
Response.AddHeader("Content-Disposition", "attachment;filename=" + strFileName);

//開始輸出讀取到的檔案
Response.BinaryWrite(b);

//一定要加入這一行,否則會持續把Web內的HTML文字也輸出。
Response.End();

[Web Form] google站內搜尋FancyBox應用

有了Google後,現在站內搜尋很方便

照著Google的方法,一步一步走,然後貼上程式碼就OK了

Google自訂搜尋 ,但這不是我要說的!


然後也因為很方便,所以樣式反而都是公版

以下是用FancyBox應用,而達成自訂Google搜尋樣式

也有浮動視窗的效果。

首先你需要先去Google申請ID,上方連結有

再來再去下載 fancybox

之後你要確定你會點程式!

1.在你的專案裡開一個新的webform在body加入

    <form id="form1" runat="server">
    <div>
        <div id="cse" style="width: 100%;">Loading</div>
        <script src="http://www.google.com/jsapi" type="text/javascript"></script>
        <script type="text/javascript">
            function parseQueryFromUrl() {
                var queryParamName = "q";//接收參數的名稱
                var search = window.location.search.substr(1);
                var parts = search.split('&');
                for (var i = 0; i < parts.length; i++) {
                    var keyvaluepair = parts[i].split('=');
                    if (decodeURIComponent(keyvaluepair[0]) == queryParamName) {
                        return decodeURIComponent(keyvaluepair[1].replace(/\+/g, ' '));
                    }
                }
                return '';
            }

            google.load('search', '1', { language: 'zh-TW' });
            google.setOnLoadCallback(function () {
                var customSearchControl = new google.search.CustomSearchControl('google申請的ID');

                customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
                customSearchControl.draw('cse');
                var queryFromUrl = parseQueryFromUrl();
                if (queryFromUrl) {
                    customSearchControl.execute(queryFromUrl);
                }
            }, true);
        </script> 
        <link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />
    </div>
    </form>

2.在你有搜尋欄的那個頁面加入

<link href="Scripts/jquery.fancybox-1.3.4.css" rel="stylesheet" />
<script src="Scripts/jquery.fancybox-1.3.4.pack.js"></script>

3.設定你TEXT的ID,跟Button onclick的function

    <form>
        <input type="text" id="txtSearch" placeholder="Search" />
        <button type="button" onclick="search()">SEARCH</button>
    </form>

4.同搜尋欄那一頁,寫function

<script>
    function search() {
        var q = $("#txtSearch").val();
        if (q != "") {
            $.fancybox({
                'href': '剛剛新增的頁面.aspx?q=' + q,//傳遞的參數
                'type': 'iframe',
                'padding': 0,
                'closeBtn': false,
                'width': 690,
                'height': 500,
                'overlayOpacity': '0.6'
            });
        }
    }
</script>

設定完後,你就可以用你自己的搜尋框去站內搜尋了。

參考:冠譯

2014年10月23日 星期四

在沒有placeholder的時候,TEXT的提示字

就如題,現在HTML5,TEXT裡有placeholder可以用

以前沒有這功能時,用JavaScript實現

<input type="text" name="search" id="search" value="" onfocus="onfocusState()" onblur="onfocusState()"/>
     
function onfocusState() {
    if (document.getElementById("search").value == "請輸入關鍵字....") {
        document.getElementById("search").value = "";
    } else if (document.getElementById("search").value == "") {
        document.getElementById("search").value = "請輸入關鍵字....";
    } else { }
}

2014年10月13日 星期一

[Web Form] 下拉選單jQuery塞值,後台取不到值

因為舊站台翻新

原本下拉選單要postback回後台取值方式

都改成jQuery的方式,在前台轉換


在前台submit到後台取值存檔時

<asp:Button runat="server" Text="確定送出" OnClick="submit" /> 

下拉選單

<select name="selectid" id="selectid" runat="server"></select>

在後台

string select_id = selectid.Value;

竟然是取不到值的,其實這邊我也不知道為什麼!

如果有哪位高手大大,知道的話,麻煩留言告知小弟一下,感謝!


最後解決的方式是

先建一個hidden,在下拉選單轉換時

<input type="hidden" name="hidselectid" id="hidselectid" runat="server" />
var selectid = $('#ctl00_ContentPlaceHolder1_selectid');
selectid.change(function () {
    $('#ctl00_ContentPlaceHolder1_hidselectid').val(selectid.val());
});

把值塞到hidden裡,這樣後台就可以取到hidden裡的值來用了。

2014年10月7日 星期二

jQuery 取 title ie8 error

原本很簡單的,不換頁置換title

  1.  //ie8 ok
  2.  var t_title = $("title").html();
  3.  //ie8 ok
  4.  t_title = t_title.replace("old title","new title");
  5.  //ie8 error
  6.  $("title").html(t_title);

在IE8上卻無法執行:
0x8000ffff - JavaScript 執行階段錯誤: 對方法或內容存取發出非預期的呼叫。
在下面的第四行上出現:

  1. append: function() {
  2.       return this.domManip(arguments, true, function( elem ) {
  3.              if ( this.nodeType === 1 ) {
  4. this.appendChild( elem );
  5.              }
  6.       });
  7. },

google了一下後,找到了答案

  1.  $("title").html(t_title);

置換成

  1.  $(document).attr("title", t_title);

參考:男丁格爾

2014年10月6日 星期一

alert 中換行!

一般來說 C# 字串中換行

我們會寫成 "這是一個\n字串"

但這在alert("這是一個\n字串"); 這樣是不行的!

要把"\n"改今"\\r",這樣就OK了


該死的n,害我的一直alert不出來!!

2014年10月1日 星期三

Cookie 中文存取

中國文字博大精深,所以連Cookie存取都要先編碼((咦

HttpContext context = HttpContext.Current;
HttpCookie acookie = new HttpCookie("admin");
acookie.Values["uname"] = HttpUtility.UrlEncode("中文");//編碼
context.Response.AppendCookie(acookie);


HttpCookie aCookie = context.Request.Cookies["admin"];

HttpUtility.UrlDecode(aCookie.Values["uname"]).ToString()//解碼