2

I'm working with a legacy project in C# (.NET 2.0). In this project there are two validationgroups. One for custom login control and one for users to submit to a newsletter. The problem I ran into is that when a user submits to subscribe to a newsletter some custom code is triggered in the page_prerender() method which only should be triggered when a user tries to login.

I have been looking for a solution to recognize which of the two groups is used on postback so I can ignore the custom code when needed. My idea was to try and check which of the two validation groups is being used to validate. Unfortunately after spending a fruitless few hours on google I've not been able to find anything to let me know how to actually known which validationgroup is used when validating. Is there any way to find out?

<asp:Button ID="btn_newsletter" 
            runat="server" 
            Text="Verzend" 
            ValidationGroup="newsLetter" 
            meta:resourcekey="bnt_newsletter"
            OnClick="handleNewsLetter"
            CssClass="roundedButtonBig" 
 />


<asp:Button ID="LoginButton" 
            runat="server" 
            CommandName="Login" 
            Text="Inloggen" 
            ValidationGroup="lgnUser" 
            meta:resourcekey="LoginButtonResource1" 
            CssClass="roundedButtonBig" 
 />

The following code should only trigger when the LoginButton is pressed and it needs to be done on Pre_render(). Or alternatively pass the correct ValidationGroup (where now null is passed).

protected void Page_PreRender(object sender, EventArgs e)
{

    //Register custom ValdiationErrorService added errors to JavaScript so they can be added into the popup.
    ValidationErrorService.RegisterServerValidationMessageScript(Page, null);

}
TheQui
  • 31
  • 4

1 Answers1

0

to check which validation group is valid, call:

Page.Validate(“newLetter”);

then check

Page.IsValid;

this will return the value. Scott Gu has more on his blog

edit you are also wanting to know which button was clicked within the prerender event it sounds like as well. While you can't find that out from the parameters passed into the page prerender, you can rely on the button events occuring prior to the page_prerender event. within the aspx pages code behind, create a member variable. this variable will be used to denote if the prerender logic should be executed.

next, within the click events of the two buttons, set that local variable to denote if that button should fire the logic you want in the page_prerender event.

last, check your local variable within the page_prerender method, and encapsulate your logic within an if statement based upon your new member variable.

Happy Trails!

Community
  • 1
  • 1
Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91
  • Thanks for the quick answer. Unless I misunderstand your solution that will just return true or false if the validation group passed. Which will be false if: a) it's used and not entered correctly or b) not used. So then I still don't know it's actually used and just entered incorrectly or not used and the other form was used. – TheQui Jun 14 '11 at 13:24
  • Can you create a flag within your asp page that is set on the button click event? you can then check that value in the page_prerender, because of how button events occur before the prerender event. – Nathan Tregillus Jun 14 '11 at 13:35
  • I was thinking about a solution like that as well. Unfortunately one button and onclick event happen in the masterpage and the other on a single page. I would need to become creative to remember this then. I was hoping to be able to see the validationgroup just as .NET uses it to automatically trigger the right validation. I am guessing this is harder then I thought. If all fails I will try that work-around. – TheQui Jun 14 '11 at 13:48
  • Actually disregard my previous comment. It's dead easy to do that despite it being in two different classes. I used that solution now. Thanks for the help :D – TheQui Jun 14 '11 at 13:57