第一次在UpdatePanel中回发加载控件/验证器时,如何使用自定义ValidatorUpdateDisplay函数? [英] How to use a custom ValidatorUpdateDisplay function when the controls / validators are loaded on postback in an UpdatePanel the first time?

查看:94
本文介绍了第一次在UpdatePanel中回发加载控件/验证器时,如何使用自定义ValidatorUpdateDisplay函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET中,当使用验证控件(即RequiredFieldValidator)时,客户端框架将执行JS函数Page_ClientValidate.此函数将验证页面上(给定的ValidationGroup的)所有控件,并使用验证器控件的span标记的DOM元素的参数调用JS函数ValidatorUpdateDisplay.

In ASP.NET when using validation controls (i.e. RequiredFieldValidator) the client sided framework will execute the JS function Page_ClientValidate. This function will validate all controls on the page (of the given ValidationGroup) and call the JS function ValidatorUpdateDisplay with a parameter of the DOM element of the span tag of the validator control.

ValidatorUpdateDisplay根据验证结果来切换span标记的可见性.

ValidatorUpdateDisplay toggles the visibility of the span tag depending on the result of the validation.

在我的Web应用程序中,我重写了ValidatorUpdateDisplay JS函数,以在验证场景中提供更多功能(即控件周围的红色边框,显示第一个失败的控件上的弹出窗口并滚动至该控件).

In my web application I've overridden the ValidatorUpdateDisplay JS function to provide more functionality on the validation scenario (i.e. red borders around the controls, showing popover on the first failed control and scrolling to it).

现在,此方法非常有效,直到在UpdatePanel中进行回发后第一次显示我的控件(包括提交"按钮)为止.

Now this works very well until my controls (incl. submit button) are shown the first time after a postback in an UpdatePanel.

<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="upTest" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="bShow" runat="server" UseSubmitBehavior="false" Text="SHOW" OnClick="bShow_Click" />
        <asp:Panel ID="pContent" runat="server" Visible="false">
            <asp:TextBox ID="tbTest" runat="server" />
            <asp:RequiredFieldValidator ID="rfvTest" runat="server" ControlToValidate="tbTest" Text="Not valid" />
            <asp:Button ID="bTest" runat="server" UseSubmitBehavior="false" Text="TEST" />
        </asp:Panel>
    </ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
    function ValidatorUpdateDisplay(val) {
        debugger; // this will not be reached
    }
</script>

protected void bShow_Click(object sender, EventArgs e)
{
    this.pContent.Visible = true;
}

初始加载后,按bShow以显示pContent. 现在,如果将tbTest.Text留为空白并按bTest,则应输入覆盖的ValidatorUpdateDisplay函数,但是它将输入框架的功能并在rfvTest中显示无效".

After initial load press bShow to display pContent. Now, if you leave tbTest.Text empty and press on bTest it should enter the overridden ValidatorUpdateDisplay function, however it enters the function of the framework and displays "Not valid" from rfvTest.

如果将pContent.Visible更改为true并在初始加载后按bTest,将发生所需的效果:它将进入自定义的ValidatorUpdateDisplay功能,并且不会显示无效".

If you change pContent.Visible to true and press bTest after initial load the desired effect will happen: It will enter the custom ValidatorUpdateDisplay function and not display "Not valid".

如果将按钮bTestUpdatePanel中移出,问题仍然存在.

If you move the button bTest out of the UpdatePanel the problem persists.

我如何使其在UpdatePanel内部工作?

How can I make it work inside an UpdatePanel?

推荐答案

ASP.NET使用惰性加载方法在第一次需要时插入ValidatorUpdateDisplay函数,因此在我的示例中,它将在之后加载该函数. UpdatePanel的回发.

ASP.NET uses a lazy loading approach to insert the ValidatorUpdateDisplay function when it needs it the first time, hence in my example it will load the function after the postback of the UpdatePanel.

这将覆盖我自己的ValidatorUpdateDisplay函数实现,因为它将在页面末尾插入该函数.

This will override my own implementation of the ValidatorUpdateDisplay function, because it's inserting the function at the end of the page.

有一个肮脏的解决方法,我只是在始终有效的初始负载上插入了一个空的CustomValidator:

There is a dirty workaround, I just inserted an empty CustomValidator on initial load that is always valid:

<asp:CustomValidator runat="server" />

我希望有一个更清洁的解决方案.

I wish there was a cleaner solution.

这篇关于第一次在UpdatePanel中回发加载控件/验证器时,如何使用自定义ValidatorUpdateDisplay函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆