2014年10月24日 星期五

使用 FormsAuthentication.SetAuthCookie

使用FormsAuthentication.SetAuthCookie

var acct = "spring";
FormsAuthentication.SetAuthCookie(acct, false);

2014年10月22日 星期三

JavaScript Function (匿名函數)

this in C#

//直譯器載入js時,會先處理所有的宣告,包含變數與函數宣告,接下來才執行程式,因此尚未執行到匿名函數時,無法調用
//一般函數宣告方式,同一個scope的任何地方都可使用func1
func1();
function func1(){
  console.log('func1 => ' + 'type of func1 : ' + typeof func1);
}

//func2()必須在指定後才能調用,雖然是具名函數,但表示式的寫法,只會提升變數的宣告,並沒有函數的定義,所以func2()還是得像匿名函數一樣先指定才可調用   
var func2 = function func2 (){
  console.log('func2');
};
func2();

//匿名函數func3()必須在指定後才能調用 
var func3 = function(){
  console.log('func3');
};
func3();

//匿名函數,占用空間在執行完會馬上釋放
( function () { 
console.log('func4');
}()); //立即執行

//callback(回調函數)
$("#btn").click(function() {
  console.log('btn');
}); 

this在C#與JavaScript的不同

this in C#

class Program
    {
        static void Main(string[] args)
        {
            Lilo lilo = new Lilo();
            //lilo.strStitch 指向 類別Lilo的 public string strStitch;
            Console.WriteLine("{0} 是我的家人 !!", lilo.strStitch);
            Console.ReadLine();
        }
        class Lilo
        {
            public string strStitch;
            public Lilo()
            {
                string strStitch;
                strStitch = "STITCH";
                //this.strStitch 指向 public string strStitch;
                this.strStitch = strStitch;
            }
        }
    }

this in JavaScript

var lilo = {
  stitch : 'STITCH',
  f: function(){
    console.log(this.stitch + " 是我的家人 !!");
  }
};
//調用f函式後,f內的this指向點f()前面的lilo
lilo.f();

ASP.NET MVC Web API 資料庫處理 using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Net.Http.Formatting;
using System.Data;
using MvcMusicStore.Models;
using System.Data.Entity;
using System.Configuration;
using System.Data.SqlClient;

namespace MvcMusicStore.Controllers
{
    public class ExtJSApiController : ApiController
    {     
        public dynamic ReviewDataGet(FormDataCollection form)
        {
            string conn = ConfigurationManager.ConnectionStrings["MvcMusicStoreContext"].ConnectionString;
            DataTable dt = new DataTable();
            string strCheck = form.Get("x");
            string strAlbumID = form.Get("AlbumID");
            string strTitle = form.Get("Title");
            using (SqlConnection sqlConnection = new SqlConnection(conn))
            {
                string strSql = "SELECT * FROM Albums WHERE AlbumID LIKE @AlbumID AND Title LIKE @Title";

                SqlCommand sqlCommand = new SqlCommand(strSql, sqlConnection);
                sqlCommand.Parameters.AddWithValue("@AlbumID", "%" + strAlbumID + "%");
                sqlCommand.Parameters.AddWithValue("@Title", "%" + strTitle + "%");
                sqlConnection.Open();
                dt.Load(sqlCommand.ExecuteReader());
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            return new ApiResponse()
            {
                success = true,
                msg = "",
                ds = ds
            };
        }
        public dynamic AlbumComboGet()
        {
            DataTable dt = new DataTable();
            using (SqlConnection sqlConnection = new SqlConnection(conn))
            {
                string strSql = "SELECT AlbumID AS 'TEXT',AlbumID AS 'VALUE' FROM Albums";
                SqlCommand sqlCommand = new SqlCommand(strSql, sqlConnection);
                sqlConnection.Open();
                dt.Load(sqlCommand.ExecuteReader());
            }
            return dt;
        }
    }
    public class ApiResponse
    {
        public DataSet ds { get; set; }
        public string msg { get; set; }
        public bool success { get; set; }
    }
}


2014年10月6日 星期一

ASP.NET MVC 使用 FullCalendar

好用的Calendar   FullCalendar


Model

public class CALENDAR
{
  public string title { get; set; }
  public string start { get; set; }
  public string end { get; set; }
  public string url { get; set; }
}

View

@{
    ViewBag.Title = "Calendar";
}

@section featured {

<link href="@Url.Content("~/Scripts/fullcalendar/lib/cupertino/jquery-ui.min.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Scripts/fullcalendar/fullcalendar.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/fullcalendar/lib/moment.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/fullcalendar/lib/jquery.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/fullcalendar/fullcalendar.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/fullcalendar/lang-all.js")"></script>

<style>
    body {
        margin: 40px 10px;
        padding: 0;
        font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
        font-size: 14px;
        overflow:scroll !important;
    }

    #calendar {
        max-width: 900px;
        margin: 0 auto;
    }
</style>

   <div id='calendar'></div>
<script>

    $(document).ready(function () {
        $('#calendar').fullCalendar({
            theme: true,
            lang: 'zh-tw',
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            editable: false,
            eventLimit: true, // allow "more" link when too many events
            events: {
                type: 'POST',
                url: '../api/TP1025CalendarGet/CalendarData',
                error: function () {
                    alert('資料存取失敗!');
                },
                success: function (response) {
 
                }
            }
        });

    });

</script>
}

Controller

public class TP1025CalendarController : Controller
{
  //
  // GET: /TP1025Calendar/
  public ActionResult Index()
  {
      return View();
  }
}

Web API

public class TP1025CalendarGetController : ApiControllerWebBase
{
    public List CalendarData(FormDataCollection form)
    {
        List calendar = new List();

        foreach (DataRow dr in response.ds.Tables[0].Rows)
        {
            calendar.Add(new CALENDAR { title = dr["title"].ToString(), start = dr["start"].ToString(), url = "" });
        }
        return calendar;
    }
}