Contents tagged with c#
-
Last GUID used up - new ScottGuID unique ID to replace it
You might have heard in recent news that the last ever GUID was used up. The GUID {FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF} was just consumed by a soon to be released project at Microsoft. Immediately after the GUID's creation the word spread around the Microsoft campuses around the globe. Microsoft's approximately 100,000 worldwide employees then started blogging, tweeting, and facebooking about the dubious "achievement." The following screenshot shows GUIDGEN (the Windows tool for creating GUIDs) with the last ever GUID.
-
Introducing SmartyRoute: A smarty-ier way to do routing in ASP.NET applications
Update 9/9/2012: Scott Hanselman has just blogged about the new Friendly URL system for ASP.NET Web Forms. It's a much fancier, and admittedly much smarty-ier way to do routing and other features for Web Forms. Also, that feature will be officially supported, unlike my original sample code!
-
The String or the Cat: A New .NET Framework Library
For years applications have been built that accept user input. Most user input starts out as a string. Strings are a universal representation of arbitrary data coming into a computer. However, most data does not remain as a string for very long. User input often ends up getting parsed or converted into another data type, such as an integer, Boolean value, or a date.
-
How to Allow Generic Controls in ASP.NET Pages
Did you ever want to have a Repeater<Customer> control on your page? And its events were all strongly typed to recognize that each row was bound to a Customer object? Well, I came up with a way of doing it using some lesser known features of ASP.NET.
-
Using C# 3.0 Anonymous Types as Dictionaries
During a design meeting for a new feature in ASP.NET we had a requirement that a new method accept a dictionary of name/values pairs. An obvious solution is to have the method accept a parameter of type IDictionary (or its generic cousin):
-
Changing the request culture for globalization and localization
In ASP.NET 2.0 we added several new features to make globalizing and localizing an application easier than ever before. One of the coolest features is the ability to declaratively localize entire controls as well as individual properties of controls. However, localization would be useless without the ability to choose which language and culture you want to display to the user. In many sites the user gets to choose their own language through some customization interface. So how does an ASP.NET developer set the culture?
There are three ways that I can think of that you can use to set the culture of a page:
- Set it in the web.config's <globalization> section. This is a great option if you want to force an entire site to have the exact same behavior when it comes to selecting a culture. Set the values of Culture and UICulture to "auto" to have ASP.NET auto-detect the browser's culture through the Accept-Language request header.
- Set it in the <%@Page %> directive. This works in the same way as the <globalization> section but is per page, including the auto-detect logic.
- For the most advanced customization of the selected culture you can override the page's InitializeCulture method and set the page's Culture and UICulture properties through code. The InitializeCulture method was added in ASP.NET 2.0 specifically for this scenario. We had to introduce a method that executes before the control tree is built such that when controls have their properties set they will use the appropriate resource set. You can place all this code in a single page base class and use the pageBaseType attribute of the <pages> section in web.config to have it be the base class for all pages in the application.
But how about setting the culture in the page's Init or Load phases? As it turns out, there are no phases of the page and control lifecycle that execute early enough so that every component on the page will realize the new culture settings. By the time Init happens, all the top-level controls on the page already have their properties set. Only dynamic controls, such as those inside templates, will realize the new culture settings since they typically get added as late as the Load or PreRender phases. InitializeCulture executes before the page and control lifecycle so that everyone can see its effects.
-
Testing your ASP.NET control (part 1 of hopefully many): ViewState
A typical ASP.NET server control will store at least some of its properties in ViewState. For example, the Label control saves the value of its Text property in ViewState so that on following postbacks the value does not need to be explicitly set again. In the first part of this series (which I hope will be extensive) we'll see how to write general unit tests for a control, and then write a unit test that ensures a property is being saved in ViewState.
-
Using ObjectDataSource to do the dirty work for your custom data source
Since ASP.NET 2.0 was released in 2005 many of you have taken advantage of the new data source control controls. Drop a GridView on the page. Bind it to a data source. Check a few checkboxes. Run the page. Go home early!
-
Attributes to consider applying when writing a custom control
Almost every custom control has at least one additional public property, and that public property as well as the control itself should probably have at least a few attributes applied to them. Attributes tell the designer host (Visual Studio) or the parser (ASP.NET) interesting things about your control that might not be evident from just its name and its type. Launch any decent class browser tool and you'll see that every control that shipped in ASP.NET and ASP.NET AJAX has several attributes on it as well as their properties and events.
-
From the Suggestion Box: Reusing object instances with ObjectDataSource
From the suggestion box Marc Brooks asks: