开发者

Get the validationgroup that is used on a postback

开发者 https://www.devze.com 2023-03-12 15:01 出处:网络
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

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:Butt开发者_运维技巧on 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);

}


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号