Check if Cookie Exists

Sometimes you still need to know if Cookie exists in Response. Then you can check if cookie key exists: HttpContext.Current.Response.Cookies.AllKeys.Contains(“myCookie”) More info can be found here. In my case I had to modify Response Cookie in Application_EndRequest method in Global.asax. If Cookie doesn’t exist I don’t touch it: string name = “myCookie”; HttpContext context = … Read more

Loop through all controls on asp.net webpage

I rather like David Finleys linq approach to FindControl http://weblogs.asp.net/dfindley/archive/2007/06/29/linq-the-uber-findcontrol.aspx public static class PageExtensions { public static IEnumerable<Control> All(this ControlCollection controls) { foreach (Control control in controls) { foreach (Control grandChild in control.Controls.All()) yield return grandChild; yield return control; } } } Usage: // get the first empty textbox TextBox firstEmpty = accountDetails.Controls .All() .OfType<TextBox>() … Read more

How to use Dependency Injection with ASP.NET Web Forms

UPDATE 2019: With the introduction of Web Forms 4.7.2, there is now better support for DI. This invalidates the below. See: Wiring up Simple Injector in WebForms in .NET 4.7.2 You can use automatic constructor injection by replacing the default PageHandlerFactory with a custom one. This way you can use an overloaded constructor to load … Read more