About the author

Steven Harmansteven harman :: makes sweet software with computers!

For recent posts and more about me, scroll to the bottom.

Subscribe

  • Subscribe to my feed. via RSS
  • Subscribe via email via email

News

Badges

  • Subtext Project
  • Support Subtext

CSS Friendly Control Adapters, CreateUserWizard, and ViewState... Oh My!

So you're using the CSS Friendly Control Adapters to generate lean, mean markup for your asp.net 2.0 controls, awesome! And you're also using some of the new Login controls to cut down on the hand-rolled authentication/authorization code, great. Do you also have ViewState disabled?

Are you also having trouble getting values out of the controls during PostBack processing? I was!

My scenario

I was using the CSS Friendly Control Adapters and the Login controls without ViewState. Specifically I was using the CreateUserWizard and it's ContentTemplate to generate a sleek user registration UI. I had attached a LoginCancelEventHandler to the wizard's CreatingUser event so I could do some custom processing of the entered data, like so:

protected override void OnInit(EventArgs e)
{
  base.OnInit(e);
  RegisterNewUser.CreatingUser += 
    new LoginCancelEventHandler(RegisterNewUser_CreatingUser);
}

private static void RegisterNewUser_CreatingUser(
  object sender, EventArgs e)
{
  CreateUserWizard userWizard = (CreateUserWizard) sender;
  userWizard.Email = userWizard.UserName;
}

Sorry about he lousy formatting... I was trying to make it blog-friendly.

The problem is userWizard.UserName was returning an empty string, which is odd because the user was created with all of the values (including the user name) I entered on the page. If I turned off the CSS Adapters then everything worked as expected.

WTF?

I did a little reflector-ing and found that the CreateUserWizard control relies on ViewState and apparently that doesn't play well with the CSS Adapters.

Not to worry, there is another way. We need to get the UserName TextBox control from the the wizard's content template's Controls collection, like this:

private static void RegisterNewUser_CreatingUser(
  object sender, EventArgs e)
{
  CreateUserWizard userWizard = (CreateUserWizard) sender;
  TextBox userName = (TextBox)
    userWizard.CreateUserStep.ContentTemplateContainer
      .FindControl("UserName");
  userWizard.Email = userName.Text;
}

I didn't take the time to digĀ into it, but I would guess this is similar to how the Membership provider/control code is wired up - as to avoid the ViewState issues. But I've been wrong before, once. :)

The gist is, you're probably better to use this more invasive code to get the values back out of the Login controls, rather than using the Properties. You could even build a little helper method to save yourself a few keystrokes and provide a hook from some unit testing.

What others are saying.

# CSS Friendly Control Adapters, CreateUserWizard, and ViewState - Oh My
Gravatar DotNetKicks.com
Apr 11, 2007
You've been kicked (a good thing) - Trackback from DotNetKicks.com
# re: CSS Friendly Control Adapters, CreateUserWizard, and ViewState... Oh My!
Gravatar Haacked
Apr 12, 2007
I wish they used Control State for that sort of thing.
# re: CSS Friendly Control Adapters, CreateUserWizard, and ViewState... Oh My!
Gravatar Steve Harman
Apr 13, 2007
Phil, I totally agree with you. I mean, isn't this exactly why they came up with the Control State mechanism to begin with?
# re: CSS Friendly Control Adapters, CreateUserWizard, and ViewState... Oh My!
Gravatar Bryan Peters
Apr 13, 2007
I came across this same problem about a month ago. My fix was to remove the CreateUserWizard CSSAdapter. I figured it was easier to let someone get a table than to try to figure this one out.
# re: CSS Friendly Control Adapters, CreateUserWizard, and ViewState... Oh My!
Gravatar Steve Harman
Apr 14, 2007
Bryan, I actually came across another issue with the CSS Adapters and the Create User Wizard... and so have several other devs.

I'm planning to write up a post about the 2nd issue in hopes it will draw some more attention and we'll get a fix.
# re: CSS Friendly Control Adapters, CreateUserWizard, and ViewState... Oh My!
Gravatar Ken Awamura
Jun 04, 2007
i'm actually experiencing an even worst scenario: http://forums.asp.net/p/1118080/1738298.aspx#1738298

i really hope that in the future we won't have to plug such things and aspnet controls just to render good html ... it's supposed to be RAD and i'm loosing my time trying to make aspnet to render standards? it's a shame
# re: CSS Friendly Control Adapters, CreateUserWizard, and ViewState... Oh My!
Gravatar Steve Harman
Jun 04, 2007
@Ken: You might want to checkout the new home of the CSS Adapters on Code Plex. There have already been a ton of fixes and some new features added to the code base, and you can get always get the latest bits via the download section.

Good Luck!
Comments have been closed on this topic.