Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Tired of Hibernate? Try JDBI in your code JDBI Quick sample ICategoryDAO.java : create a data access interface (implement is not required) package com.prhythm.erotic.task.data.... filetracker error ftk1011 FileTracker error: ftk1011 : an error occurs on converting project from visual studio 2008(or under?) to visual studio 2010, and with some ...
Stats
JSRequest, Get parameters from querystring with javascript in SharePoint

Provides method to parse query string, filename, and pathname from URL

// Initialize first
JSRequest.EnsureSetup();

// Get the current file name
JSRequest.FileName: "qa.aspx"
// Get the current path name
JSRequest.PathName: "/sites/BruceDev/SitePages/qa.aspx"
// Get request query string parameter
JSRequest.QueryString["dy"];
JSRequest
JSRequest = {
    QueryString: null,
    FileName: null,
    PathName: null,
    EnsureSetup: function () {
        if(JSRequest.QueryString != null) return;
        JSRequest.QueryString = new Array();
        var queryString = window.location.search.substring(1);
        var pairs = queryString.split("&");
        for(var i = 0; i < pairs.length; i++) {
            var p = pairs[i].indexOf("=");
            if(p > -1) {
                var key = pairs[i].substring(0, p);
                var value = pairs[i].substring(p + 1);
                JSRequest.QueryString[key] = value;
            }
        }
        var path = JSRequest.PathName = window.location.pathname;
        var p = path.lastIndexOf("/");
        if(p > -1) {
            JSRequest.FileName = path.substring(p + 1);
        } else {
            JSRequest.PageName = path;
        }
    }
};
Fix for SharePoint 2010 scrolling problems for Chrome
Execute on page loaded
function bodyOnLoadWrapperForChrome() {
    //verify we have finished loading init.js
    if (typeof (_spBodyOnLoadWrapper) != 'undefined') {
        //verify we have not already initialized the onload wrapper
        if (typeof (_spBodyOnloadCalled) == 'undefined' || !_spBodyOnloadCalled) {
            //initialize onload functions
            _spBodyOnLoadWrapper();
        }
    }
    else {
        //wait for 10ms and try again if init.js has not been loaded
        setTimeout(bodyOnLoadWrapperForChrome, 100);
    }
}
CSS alpha
.alpha50 {
    filter: alpha(Opacity=50, Style=0); /*for ie*/
    -moz-opacity: 0.5; /* Moz + FF */
    opacity: 0.5; /* CSS3的標準語法,FOR支援CSS3的瀏覽器(FF 1.5)*/
}
Linq: Loop extension
using System;
using System.Collections.Generic;
using System.Text;

namespace System.Linq
{
    public static class GenericEachExtension
    {
        public static IEnumerable<T> Each<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach (T t in source)
            {
                action.Invoke(t);
            }
            return source;
        }

        public static IEnumerable<T> Each<T>(this IEnumerable<T> source, Func<T, bool> action)
        {
            foreach (T t in source)
            {
                if (!action.Invoke(t)) break;
            }
            return source;
        }

        public static IEnumerable<T> Each<T>(this IEnumerable<T> source, Action<T, int> action)
        {
            int i = 0;
            foreach (T t in source)
            {
                action.Invoke(t, i);
                i++;
            }
            return source;
        }

        public static IEnumerable<T> Each<T>(this IEnumerable<T> source, Func<T, int, bool> action)
        {
            int i = 0;
            foreach (T t in source)
            {
                if (!action.Invoke(t, i)) break;
                i++;
            }
            return source;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Collections
{
    public static class EachExtension
    {
        public static IEnumerable Each<T>(this IEnumerable source, Action<T> action)
        {
            foreach (T t in source)
            {
                action.Invoke(t);
            }
            return source;
        }

        public static IEnumerable Each<T>(this IEnumerable source, Func<T, bool> action)
        {
            foreach (T t in source)
            {
                if (!action.Invoke(t)) break;
            }
            return source;
        }

        public static IEnumerable Each<T>(this IEnumerable source, Action<T, int> action)
        {
            int i = 0;
            foreach (T t in source)
            {
                action.Invoke(t, i);
                i++;
            }
            return source;
        }

        public static IEnumerable Each<T>(this IEnumerable source, Func<T, int, bool> action)
        {
            int i = 0;
            foreach (T t in source)
            {
                if (!action.Invoke(t, i)) break;
                i++;
            }
            return source;
        }

        public static IEnumerable Each(this IEnumerable source, Action<object> action)
        {
            foreach (object t in source)
            {
                action.Invoke(t);
            }
            return source;
        }

        public static IEnumerable Each(this IEnumerable source, Func<object, bool> action)
        {
            foreach (object t in source)
            {
                if (!action.Invoke(t)) break;
            }
            return source;
        }

        public static IEnumerable Each(this IEnumerable source, Action<object, int> action)
        {
            int i = 0;
            foreach (object t in source)
            {
                action.Invoke(t, i);
                i++;
            }
            return source;
        }

        public static IEnumerable Each(this IEnumerable source, Func<object, int, bool> action)
        {
            int i = 0;
            foreach (object t in source)
            {
                if (!action.Invoke(t, i)) break;
                i++;
            }
            return source;
        }
    }
}
executeOrDelayUntilScriptLoaded
Executes the specified function if the file containing it is loaded; otherwise, adds it to the pending job queue.
ExecuteOrDelayUntilScriptLoaded(func, depScriptFileName)
or
SP.SOD.executeOrDelayUntilScriptLoaded(func, depScriptFileName)
<script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(function() {
        alert('sp.js loaded');
    }, 'sp.js');

    SP.SOD.executeOrDelayUntilScriptLoaded(function() {
        alert('jquery.js loaded');
    }, 'jquery.js');
</script>
Page load event handler on sharepoint
<script type="text/javascript">
    _spBodyOnLoadFunctionNames.push("onPageLoadAction");

    function onPageLoadAction() {
        // code here
    }
</script>
new operator/modifier/constraint

new Operator Used to create objects and invoke constructors.

// Create objects and invoke constructors
Class1 obj  = new Class1();

// Create instances of anonymous types
var employee = new { EmpID = 3, Name = "Bruce", Gender = Gender.Male};

// invoke the default constructor for value types
int i = new int();

// invoke the default constructor for value types with default value
int i = 0;

new Modifier Used to hide an inherited member from a base class member.

class ParentClass
{
    public int x = 5;
    public void Work()
    {
        for (int i = 0; i < x; i++) Console.WriteLine("Parent work : " + i);
    }
    public decimal Caculate()
    {
        return x + x;
    }
}

class ChildClass : ParentClass
{
    public new int x;
    public new void Work()
    {
        for (int i = 0; i < x; i++) Console.WriteLine("Child work : " + i);
    }
    public new decimal Caculate()
    {
        return base.x + this.x;
    }
}

class Program
{
    static void Main(string[] args)
    {
        ParentClass p = new ParentClass();
        p.Work();
        // Parent work : 0
        // Parent work : 1
        // Parent work : 2
        // Parent work : 3
        // Parent work : 4
        Console.WriteLine(p.Caculate());
        // 10

        Console.WriteLine("================");

        ChildClass c = new ChildClass() { x = 3 };
        c.Work();
        // Child work : 0
        // Child work : 1
        // Child work : 2
        Console.WriteLine(c.Caculate());
        // 8
    }
}

new Constraint Used to restrict types that might be used as arguments for a type parameter in a generic declaration.

class MyCollection<T> where T : new()
{
    // Type T must have a public parameterless constructor
}

class MyCollection2<T> : IEnumerable<T> where T : IDisposable, new()
{
    // Type T implements IDisposable and must have a public parameterless constructor,
    // the new() constraint must at last
}
Redirect webpart in sanboxed solution
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace MyWebpart
{
    [ToolboxItemAttribute(false)]
    public class ConsentsApprover : WebPart
    {
        protected override void CreateChildControls()
        {
            Controls.Add(new Literal()
            {
                Text = @"<script type=""text/javascript"">location='your_redirect_url';</script>"
            });
        }
    }
}