Archives
-
ASP.NET MVC Design Philosophy
This week the first preview of the ASP.NET MVC framework was released to the web as part of the ASP.NET 3.5 Extensions CTP Preview. It's been a few months since we started coding and as the lead developer on the project I'd like to share my thoughts on the design of this framework. This isn't a post about why MVC is great. Instead, it's a post about what we did to make MVC happen in ASP.NET.
-
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.
-
DevConnections/ASPConnections Slides and Code
As a follow up to my previous post, here are links to Scott Hanselman's posts:
-
DevConnections/ASPConnections 2007 in Vegas
Next month I'll be co-speaking with Scott Hanselman at Microsoft ASP.NET Connections in Las Vegas. Completely stealing from ScottHa's post, this is the schedule for Tuesday, November 6:
-
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):
-
ScriptManager's EnablePartialRendering and SupportsPartialRendering properties
In ASP.NET AJAX's ScriptManager control there are two properties that seem quite similar, but are in fact very different:
-
How ASP.NET databinding deals with Eval() and Bind() statements
A recent question on one of our internal mailing lists asked where the definition for the Bind() method is located in ASP.NET. The asker had located the definition of the Eval() method on the TemplateControl class, but the Bind() method was nowhere to be found, even using Reflector.
If you're familiar with databinding in ASP.NET 2.0, you probably know that for read-only values such as Labels you can use the Eval() statement, and for read-write values such as TextBoxes (also known as "two-way databinding") you can use the Bind() statement. Where does that Bind() statement come from?
To the surprise of many readers, there isn’t a bind method in ASP.NET! When ASP.NET parses your file and sees you're using a databinding expression (in the angle-bracket-percent-pound format, "<%# %>") it has special-case code to parse for the Bind syntax and generates some special code for it. When you use <%# Bind("Name") %> it's not a real function call. If ASP.NET parses the code and detects a Bind() statement, it splits the statement into two parts. The first part is the one-way databinding portion, which ends up being just a regular Eval() call. The second part is the reverse portion, which is typically some code along the lines of "string name = TextBox1.Text" that grabs the value back out from where it was bound.
-
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!
-
Registering scripts in ASP.NET controls (the right way)
Let's start out with an innocent control that registers a simple script file include:
-
Why don't file uploads work during async postbacks?
As many people have noticed in their AJAX-enabled pages, file uploads do not work when doing an async postback. To my knowledge there's no way to support this scenario due to the browser's security restrictions. To understand why the upload doesn't work it's important to understand how async postbacks are performed and how that's different from how the browser performs a regular postback.
-
Sys.WebForms.PageRequestManagerParserErrorException - what it is and how to avoid it
If you've used the Microsoft ASP.NET AJAX UpdatePanel control, there's a good chance you've hit the "Sys.WebForms.PageRequestManagerParserErrorException" error.
-
ASP.NET AJAX JavaScript Class Browser (take 3)
By popular demand, I am releasing another update to the ASP.NET AJAX Class Browser. The new features:
-
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:
-
How to instantiate templates (properly)
As part of my work on the ASP.NET team I've worked directly with several 3rd party control vendors and have spoken to hundreds of customers at conferences such as PDC and TechEd as well as presented on topics related to building controls and using ASP.NET in general. I've looked at the source code for literally hundreds of controls and although the controls are usually pretty darn cool, sometimes I spot some snippets code that just don't look right. That is, they don't look right to me. I'm sure they look right to whoever wrote them.
-
From the Suggestion Box: Why can't you use code expressions for properties?
From the suggestion box InfinitiesLoop asks:
-
Suggestion Box Now Open
I opened up a suggestion box so you can tell me what you want to hear about.
-
Suggestion Box
It's so hard to come up with blog topics that I've decided to outsource the whole idea creation operation. What topics do you want to hear about? As with most people, I prefer talking about my own area of expertise and not just random ramblings. Here are the general topics I'm familiar with: