Skip to content

Instantly share code, notes, and snippets.

View sebnilsson's full-sized avatar
☁️

Seb Nilsson sebnilsson

☁️
View GitHub Profile
@sebnilsson
sebnilsson / RenderSectionWebPageBaseExtensions.cs
Last active September 30, 2015 19:47
RenderSection with Default Content
using System;
using System.Web.WebPages;
public static class WebPageBaseExtensions
{
public static HelperResult RenderSection(this WebPageBase page, string sectionName, Func<dynamic, HelperResult> defaultContents)
{
if (page.IsSectionDefined(sectionName))
{
return page.RenderSection(sectionName);
var timeoutId = 0;
var scrollEvent = function() {
alert('Scroll event was fired');
};
$(window).scroll(function () {
clearTimeout(timeoutId );
timeoutId = setTimeout(scrollEvent, 500);
});
@sebnilsson
sebnilsson / gist:3832965
Last active October 11, 2015 08:47
ASP.NET MVC Detect If Index Page
string currentAction = Convert.ToString(this.ViewContext.Controller.ValueProvider.GetValue("action").RawValue);
string currentController = Convert.ToString(this.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue);
bool isIndexPage = currentAction.Equals("index", StringComparison.InvariantCultureIgnoreCase)
&& currentController.Equals("home", StringComparison.InvariantCultureIgnoreCase);
@sebnilsson
sebnilsson / sort-and-remove-duplicates.js
Last active October 11, 2015 11:28
Sort and remove duplicates
var items = [ 'ABC', '123', 'BAA' ];
items.sort(function (a, b) {
var aVal = a.val.toLowerCase();
var bVal = b.val.toLowerCase();
return ((aVal < bVal) ? -1 : ((aVal > bVal) ? 1 : 0));
});
var last = items[0];
for (var i = 1; i < items.length; i++) {
var item = items[i];
@sebnilsson
sebnilsson / is-number.js
Last active October 11, 2015 15:27
JavaScript isNumber / isNumberOrEmpty
var isNumber = function(number) {
return !isNaN(parseFloat(number)) && isFinite(number);
};
var isNumberOrEmpty = function(number) {
number = (number || 0).toString().replace(' ', '').replace(',', '.');
return !isNaN(parseFloat(number)) && isFinite(number);
};
@sebnilsson
sebnilsson / RouteCollectionAreasExtensions.cs
Created December 2, 2015 13:26
Manually map an area-specific route
public static class RouteCollectionAreasExtensions
{
public static Route MapAreaRoute(
this RouteCollection routes,
string areaName,
string routeName,
string url,
object defaults = null,
object constraints = null,
string[] namespaces = null)
@sebnilsson
sebnilsson / update-jquery-on-the-fly.js
Last active December 9, 2015 19:29
Update jQuery on-the-fly in Javascript
console.log($().jquery); // Show original jQuery-version
// Closure
var $gs = $.getScript;
// http://api.jquery.com/jQuery.noConflict/
// jQuery.noConflict( [removeAll] )
// removeAll: A Boolean indicating whether to remove all jQuery variables from the global scope (including jQuery itself).
$.noConflict(true);
@sebnilsson
sebnilsson / FileSizeHelper.cs
Last active December 11, 2015 04:38
Get human-readable file-sizes
public class FileSizeHelper
{
private static readonly string[] Units = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
private static string GetReadableFileSize(long size) // Size in bytes
{
int unitIndex = 0;
while (size >= 1024)
{
size /= 1024;
public DateTime? TryGetAssemblyCreationDate(Assembly assembly)
{
try
{
return System.IO.File.GetCreationTime(assembly.Location);
}
catch
{
return null;
}
@sebnilsson
sebnilsson / TagPrefix-Error
Last active December 13, 2015 17:58
User Controls vs. Custom Controls - Web.config - system.web.pages.controls
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Invalid or missing attributes found in the tagPrefix entry. For user control, you must also specify 'tagName' and 'src'. For custom control, you must also specify 'namespace', and optionally 'assembly'.