Thursday, August 30, 2007

Simple function to split camelCase words

The phrase "camel case" is used to describe a common method of naming variables, properties, and methods in a programming language.

It is useful for stringing several words together, while maintaining readability.  (More here)

If you ever have the need to split apart the words in a camel case word, here's just about the easiest way to do it (shown in both VB.NET and C#):

SplitCamelCase() - VB

Function SplitCamelCase(ByVal Source As String) As String()
    Return Regex.Split(Source, "(?<!^)(?=[A-Z])")
End Function

SplitCamelCase() - C#

string[] SplitCamelCase(string source) {
    return Regex.Split(source, @"(?<!^)(?=[A-Z])");


Wednesday, August 1, 2007

Never use a MultiView inside a Repeater

...Unless you want a ViewState larger than the rest of your page

First off, without the help of Fritz Onion's ViewStateDecoder, my life would have been messed up a lot longer than it was tonight, as I was searching for a reason to explain a 60K ViewState embedded in a page.

I meticulously turned off the ViewState in every server control that did not need it with no apparent effect on the ViewState size.

Googling, I came upon Frtiz's ViewState tool, which allows you to see the contents of the ViewState.  The utility actually has quite a few nice features, such as the ability to pull ViewState out of any published page -- directly.

What the tool showed me was that a lot of what I was seeing in the page was actually the ControlState, not the ViewState.  Of course, it all looks the same when you see it in the page, but there is an big difference between the two.

ControlState is something you can't turn off.  It is a control's fail-safe way of being able to save state information, just in case ViewState is disabled in the page.

So I traced almost the entire bulk of ViewState in the page to 3 MultiView controls that were sitting inside templates  of a Repeater control.  I could disable ViewState for the entire page, and it still couldn't touch that ControlState.

Who designs these things?

Anyway, I came up with a solution that allows me to keep the MultiViews, and even leave them inside the repeater.

I built a very simple MultiViewLite control, which is inherited from the MultiView, and disables the ControlView.  When I was finished, I just replaced <asp:Multiview... with <MyControls:MultiviewLite... and everything worked perfectly!

The complete code code listing for the MultiViewLite control can be found below.  It has the identical functionality of the standard MultiView control, with the exception that both ControlState and ViewState are disabled.


Public Class MultiViewLite : Inherits MultiView

    Protected Overrides Function SaveControlState() As Object
Return Nothing
End Function

    Protected Sub Control_Init(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Init
Me.EnableViewState = False
    End Sub

End Class