2014年12月31日 星期三

ashx繼承方式

public class BaseHandler : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        OnLoad(context);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    public virtual void OnLoad(HttpContext context)
    { 
    }
}
 
================================
 
public class test : BaseHandler
    {
        public override void OnLoad(HttpContext context)
        {

           //DOSomething
        }
    } 

2014年12月15日 星期一

Javascript HTML Encode & Decode

function htmlEncode(value){
  //create a in-memory div, set it's inner text(which jQuery automatically encodes)
  //then grab the encoded contents back out.  The div never exists on the page.
  return $('<div/>').text(value).html();
}

function htmlDecode(value){
  return $('<div/>').html(value).text();
}

Javascript Replace All

取代所有 " 變成 _

'a"b"c'.replace(/"/g,"_");

2014年11月17日 星期一

上傳檔案 透過 Ajax To Ashx

Script: 
 
<script type="text/javascript">
$(document).ready(function () {
  $("#Button1").click(function (evt) {
    var fileUpload = $("#FileUpload1").get(0);
    var files = fileUpload.files;

    var data = new FormData();
    for (var i = 0; i < files.length; i++) {
      data.append(files[i].name, files[i]);
    }
  
    //額外的參數 
    data.append("Key", "Value");
     
    var options = {};
    options.url = "FileUploadHandler.ashx";
    options.type = "POST";
    options.data = data;
    options.contentType = false;
    options.processData = false;
    options.success = function (result) { alert(result); };
    options.error = function (err) { alert(err.statusText); };
  
   $.ajax(options);
 
   evt.preventDefault();
  });
});
</script>
 
 
ashx: 

public void ProcessRequest(HttpContext context)
        {
          //額外參數
          string para1 = context.Request.Params.Get("key") == null  ? "" : 
            context.Request.Params.Get("key");
 
          if (context.Request.Files.Count > 0)
            {
                HttpFileCollection files = context.Request.Files;
                for (int i = 0; i < files.Count;i++ )
                {
                    HttpPostedFile file = files[i];
                    string fname = context.Server.MapPath("~/uploads/" + file.FileName);
                    file.SaveAs(fname);
                }
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write("File(s) Uploaded Successfully!");
        }

2014年10月16日 星期四

SQL Select 結果後 手動更新其中美些欄位值

DECLARE @TmpTable1 TABLE(
    FC_Title varchar(MAX) ,
    FC_Content varchar(MAX),
    Brands varchar(MAX),
    FC_ID int,
    QTY int
);    
    
WITH T AS(                      
SELECT FC_Title, FC_Content, dbo.GetForumBrands(FC_ID) AS Brands, FC_ID,
(SELECT COUNT(*) FROM Forum F LEFT JOIN UserItem UI ON F.UID=UI.UID WHERE F.FC_ID=FC.FC_ID AND F.FM_IsOpen=1) AS QTY
FROM ForumCatalog FC WHERE FC_IsOpen=1 )

Insert into @TmpTable1(FC_Title, FC_Content, Brands,FC_ID,QTY) select FC_Title, FC_Content, Brands,FC_ID,QTY from T

update @TmpTable1 set QTY = (
    select COUNT(*) from
             (select k.Id from dbo.KM_Expert k inner join KM_Expert_Estimate e on k.Id = e.ExpertId
             group by k.Id) T
 ) where FC_ID = 10

select * from @TmpTable1 ORDER BY FC_ID

2014年9月16日 星期二

解決 MasterPage Head <% ... %> 錯誤

css:
<link href="~/App_Themes/Theme1/css/grid.css" rel="stylesheet" type="text/css" />

js:
<script type="text/javascript" src='<%#ResolveUrl("~/JavaScript/jquery.cookie.js") %>'></script>

後端: 
protected override void OnLoad(EventArgs e)
{
        Page.Header.DataBind();
}

2014年7月16日 星期三

SQL 樹 遞迴 查詢所有子節點

WITH T AS
(
SELECT CID,ParentID
    , 0 AS Level
    FROM SYS_Category
    WHERE CID = 8
     UNION ALL
    SELECT c.CID,c.ParentID, Level +1
    FROM SYS_Category c
    inner JOIN T t1
    ON c.ParentID=t1.CID
)
SELECT * FROM T;

SQL 子查詢 文字累加

select u.*,
(select CONVERT(nvarchar(max),f.RID ) + ','
from UserRoleRelation f
 where u.UID = f.UID for xml path('')) role
from SYS_User u

2014年3月6日 星期四

Scroll Bottom Ajax

        //離底部有多少距離
        var bottom = parseInt ( $(window).height() / 10 );

        //是否觸發Ajax
        var isLoading = false;

            $(window).scroll(function(){
                if (!isLoading && $(window).scrollTop() + $(window).height() >= $(document).height() - bottom){
                //if (!isLoading && $(window).scrollTop() + screen.height >= $(document).height() - bottom){
                   isLoading = true;
                  
                  doSomeThing();
                }
          });



function doSomeThing(){
             // show loading icon
            $("#loadingIcon").show();

           $.ajax({
                method: "POST",
                url: "",
                dataType: "json",
                async: true,
                data: {
                },
                cache: false,
                success: function (data) {
               //新增內容
               $("#contentList").append(resultHTML);

                    //確認資料是否還有資料 是否要恢復Scroll事件
                    if( parseInt(count) > 0) {
                        isLoading = false;
                        // 取得資料索引遞增
                         AtPage++;
                    }
                    $("#loadingIcon").hide();
                },
                error: function () {
                    isLoading = false;
                    $("#loadingIcon").hide();
                }
            });
}

2014年2月6日 星期四

呼叫 IFRAME內 的 html

<iframe id = "filePage" src="" style="width:825px;height:600px"></iframe>
 
 
$("#filePage").contents().find("#myContent")

2014年2月5日 星期三

檔案類型過濾

var ext = $('#my_file_field').val().split('.').pop().toLowerCase();
if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
    alert('invalid extension!');
}

2014年1月28日 星期二

檔案大小format

function formatDataSize(kb) {
    if (kb == '')
        return "";
    kb = parseInt(kb);
    var mb = kb / 1024;
    if (mb >= 1)
        return formatFloat(mb, 2) + " MB";
    else
        return kb + " KB";
}

function formatFloat(num, pos) {
    var size = Math.pow(10, pos);
    return Math.round(num * size) / size;
}

時間format

function formatTime(sec) {
    if (sec == '')
        return "";
    sec = parseInt(sec);
    var hour = parseInt(sec / 3600);
    var min = parseInt(sec / 60);
    sec = sec % 60;
    return hour + ":" + (min < 10 ? '0' + min : min) + ":" + (sec < 10 ? '0' + sec : sec);
}

2014年1月26日 星期日

找不到圖片 使用預設圖片

            //使用預設封面
            $('#AlbumList table img').error(function () {
                $(this).attr("src", "./images/album.jpg");
            });

2014年1月23日 星期四

textbox 指標到最後

$("#keyword").focus().val('').val(keyword);

鍵盤 Key Enter 事件

    $("#keyword").keypress(function (e) {
       //enter事件
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $("#search").click();
            return false;
        } else {
            return true;
        }
    });

checkBox 設定全選

    //設定全選
    $("#selectAll").click(function () {
        var isCheck = $(this).prop("checked");
        $("input[name='photoItem']").prop("checked", isCheck);
    });

checkbox 多檔案 刪除

    //刪除檔案
    $("input[name='delete']").click(function () {
        var IDArray = [];
        $("input[name='file']:checked").each(function () {
            IDArray .push($(this).val());
        });

        if (IDArray .length == 0) {
            alert("請至少勾選一個檔案!");
            return false;
        }
        deleteFile(IDArray );
    });

checkBox只能單選

    //設定checkBox 只能單選
    $("#AlbumList table").delegate("input[name='albumItem']", "click", function () {
        var isCheck = $(this).prop("checked");
        if (isCheck) {
            $("#AlbumList table input[name='albumItem']").prop("checked", false);
            $(this).prop("checked", true);
        }
    });