Skip to content

Instantly share code, notes, and snippets.

@DTTerastar
DTTerastar / PercentComplete.cs
Created December 5, 2012 16:02
PercentComplete
public class PercentComplete : IDisposable
{
private readonly float _max;
private readonly float _min;
private readonly int _total;
private int _current;
public PercentComplete(int total)
: this(total, 0, 100)
{
public class EnsureDataBound : Control
{
private int _count;
public bool AllowMultiple { get; set; }
public bool EveryPostBack { get; set; }
public When When { get; set; }
public override void DataBind()
{
@DTTerastar
DTTerastar / Urls.cs
Last active September 27, 2016 20:53
public static string ResolveUrl(string url)
{
string appDomainAppVirtualPath = HttpRuntime.AppDomainAppVirtualPath;
return url.StartsWith("~/") ? appDomainAppVirtualPath + url.Substring(appDomainAppVirtualPath != null && appDomainAppVirtualPath.EndsWith("/") ? 2 : 1) : url;
}
public static string ResolveUrl(string url, object queryStringParams, bool skipEmptyValues = true)
{
if (queryStringParams == null) return ResolveUrl(url);
var sb = new StringBuilder(ResolveUrl(url));
@DTTerastar
DTTerastar / ExtensionMethods.cs
Created January 16, 2013 17:50
Turn Pascal cased Enums and Strings into more readable text by adding spaces
static public string ToStringAddSpaces(this Enum value)
{
string a = value.ToString();
return PascalCaseAddSpaces(a);
}
static public string[] PascalCaseToWords(string value)
{
return Regex.Split(value, "(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])");
}
public static IEnumerable<T> Unpage<T>(Func<int, int, IEnumerable<T>> func, int batchSize)
{
int skip = 0;
int count;
do
{
count = 0;
foreach (var a in func(skip, batchSize))
{
count++;
public static TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue defaultVal = default(TValue))
{
TValue ret;
return dic.TryGetValue(key, out ret) ? ret : defaultVal;
}
public static TValue GetValueOrDefault<TValue>(this StateBag stateBag, string key, TValue defaultValue = default(TValue))
{
object value = stateBag[key];
return Equals(value, null) ? defaultValue : (TValue) value;
; Variables definition
; -----------------------------------------------------------------------------
EnvGet, userProfile, USERPROFILE
Software := userProfile . "\Dropbox\software\"
; Launch or toggle program, http://lifehacker.com/5468862/create-a-shortcut-key-for-restoring-a-specific-window
; -----------------------------------------------------------------------------
ToggleWinMinimize(WindowTitle)
{
SetTitleMatchMode,2
public static TValue GetOrSet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> create)
{
TValue value;
return dictionary.TryGetValue(key, out value) ? value : (dictionary[key] = create(key));
}
public static TValue GetOrSet<TKey, TValue>(this IDictionary dictionary, TKey key, Func<TKey, TValue> create)
{
var value = (TValue) dictionary[key];
if (!Equals(value, default(TValue))) return value;
public class NavHistory : Control
{
private readonly List<Tuple<string, Action<string>>> _setters = new List<Tuple<string, Action<string>>>();
private Dictionary<string, string> _changes = new Dictionary<string, string>();
private ScriptManager _sm;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.Init += Page_Init;
@DTTerastar
DTTerastar / Asterisk.cs
Created February 14, 2013 15:30
Validate by using IValidatableObject, or by DbEntityValidationException. The asterisk control with the correct member name will show which fields need updating, while all error messages will show in ValidationSummary.
public static class AsteriskValidation
{
public static void Validate(this Page page, IValidatableObject validatableObject, ValidationContext context = null)
{
foreach (ValidationResult error in validatableObject.Validate(context))
page.Validators.Add(new ErrorValidator(error));
}
public static void Validate(this Page page, DbEntityValidationException ex, ValidationContext context = null)
{