views:

511

answers:

1

Hi, I've got a CreateUserWizard control and am performing server-side validation. Is there a way to force the CUW's error message to display from the code-behind? At the moment, if the user enters a duplicate name, the controls DuplicateUserNameErrorMessage property is displayed. However, if the user has turned javascript off, or sends a custom POST header, with invalid characters, I rely on my server-side validation to catch the error. How can I then display the same error message in the control's ErrorMessage label, instead of creating a custom label and faking it?

Edit: Just to clarify, the server side validation tests various aspects. Duplicate user was just an example of when the ErrorMessage label gets called by the control.

Thanks

+1  A: 

Update:

Here is something that will work, but requires private reflection:

void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e) {
    typeof(CreateUserWizard).GetField(
        "_unknownErrorMessage",
        System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
        .SetValue(sender, "My error message");
    e.Cancel = true;
}

Basically, you set this field to the error message you want, and CreateUserWizard picks it up. Being private reflection, it's not a 'supported' technique, but it's at least an option to consider if nothing else works.


I don't think you should have to do anything special for this to work. Generally, anything that supports client side validation has matching server side validation logic. In fact, for something like duplicate name, there is only server side validation, so I wouldn't think that turning off javascript should affect the scenario.

What exactly are you seeing when you disable javascript and you post a duplicate name? ARe you able to repro this issue with a simple page, or is there some added factors that could affect it?

I tried with a plain CUW and disabled javascript, and the duplicate user error was correctly displayed.

David Ebbo
Sorry, I think I should've been a bit more clear. The Duplicate user was just an example of the ErrorMessage label that shows. My server side validation checks for invalid characters, suspicious scripts etc.. If these get flagged, I would like to use the same ErrorMessage label to display a message. I've been able to access the ErrorMessage label in the CUW, but I can't force it to display from the code-behind.
keyboardP
How are you hooking up your server side validation? Are you using a custom validator control?
David Ebbo
I'm catching the *CreatingUser* event (of the CUW) and checking each textbox that is being submitted. The text from each textbox is placed into a custom validation function which returns false if there's anything suspicious. If this function returns false, then I would like the ErrorMessage label in the CUW to display a custom message (and I cancel the CreatingUser event).
keyboardP
I think the reason the message is not getting displayed is that the ErrorMessage label is hidden. Does it work in you do 'label.Visible=true'?
David Ebbo
Hi. No it doesn't show. However, when I did it, the page refreshed as it's a postback. Could this postback be the reason it's not showing? I tried enabling viewstate on the CUW object, but it didn't work.
keyboardP
Thank you! The reflection code solves it! Is there a reason why accessing the standard UnknownErrorMessage property exposed by the control doesn't work in this case? From what I've read, small scale reflection won't cause much of a performance hit. This, coupled with the fact that it will only ever be used in rare circumstances (i.e. intended attacks), I will use this method. Thanks again.
keyboardP