获取回发时使用的验证组

发布于 2024-11-15 17:47:48 字数 1215 浏览 4 评论 0原文

我正在处理 C# (.NET 2.0) 中的遗留项目。在这个项目中有两个验证组。一种用于自定义登录控制,另一种用于用户提交新闻通讯。我遇到的问题是,当用户提交订阅时事通讯时, page_prerender() 方法中会触发一些自定义代码,该方法仅应在用户尝试登录时触发。

我一直在寻找一种解决方案来识别回发时使用两个组中的哪一个,以便我可以在需要时忽略自定义代码。我的想法是尝试检查使用两个验证组中的哪一个进行验证。不幸的是,在谷歌上花了几个小时毫无结果后,我无法找到任何东西让我知道如何真正知道验证时使用了哪个验证组。有什么办法可以查到吗?

<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" 
 />

以下代码应该仅在按下 LoginButton 时触发,并且需要在 Pre_render() 上完成。或者传递正确的 ValidationGroup(现在传递 null)。

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);

}

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);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-11-22 17:47:48

要检查哪个验证组有效,请调用:

Page.Validate(“newLetter”);

然后检查

Page.IsValid;

这将返回值。 Scott Gu 在他的博客上有更多内容

编辑您还想知道在预渲染事件中单击了哪个按钮,听起来也很像。虽然您无法从传递到页面预渲染器的参数中找到这一点,但您可以依赖在 page_prerender 事件之前发生的按钮事件。在 aspx 页面代码后面,创建一个成员变量。该变量将用于指示是否应执行预渲染逻辑。

接下来,在两个按钮的单击事件中,设置该局部变量以指示该按钮是否应触发 page_prerender 事件中所需的逻辑。

最后,检查 page_prerender 方法中的局部变量,并根据新成员变量将逻辑封装在 if 语句中。

快乐的足迹!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文