动态验证控件在更新面板中不起作用

发布于 2024-07-30 00:06:59 字数 1438 浏览 7 评论 0原文

我动态创建验证控件并将它们添加到更新面板。 然而,客户端验证永远不会触发。

这是 aspx 文件:

<div>
    <asp:UpdatePanel ID="UpdatePanel1"  UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </ContentTemplate>
    <Triggers >
        <asp:AsyncPostBackTrigger ControlID ="Button1" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

<asp:Button ID="Button1" runat="server" Text="Button"  CausesValidation="true"/>


</div>

这是背后的代码:

Dim Survey As New Survey
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Survey.RenderPage(PlaceHolder1)
End Sub

这是创建验证控件的类:

Public Class Survey

    Public Sub RenderPage(ByVal PlaceHolder As PlaceHolder)

        Dim textbox As New TextBox
        textbox.ID = "testing"
        PlaceHolder.Controls.Add(textbox)

        Dim val As New RequiredFieldValidator
        val.ControlToValidate = textbox.ID
        val.Text = "required"
        val.EnableClientScript = True
        PlaceHolder.Controls.Add(val)

    End Sub
End Class

当您点击下一步时,客户端验证永远不会触发。 真正奇怪的是,当您将按钮包装在另一个更新面板中时,验证会触发(在 IE 和 Firefox 中,但在 Chrome 或 Safari 中则不会)。

有人知道发生了什么事吗? 我知道 Asp.net AJAX 的第一个版本不支持验证控件,但我这边一切都是最新的。

I'm dynamically creating validation controls and adding them to an update panel. However the client side validation never fires.

Here is the aspx file:

<div>
    <asp:UpdatePanel ID="UpdatePanel1"  UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </ContentTemplate>
    <Triggers >
        <asp:AsyncPostBackTrigger ControlID ="Button1" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

<asp:Button ID="Button1" runat="server" Text="Button"  CausesValidation="true"/>


</div>

Here is the code behind:

Dim Survey As New Survey
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Survey.RenderPage(PlaceHolder1)
End Sub

Here is the class that creates the validation control:

Public Class Survey

    Public Sub RenderPage(ByVal PlaceHolder As PlaceHolder)

        Dim textbox As New TextBox
        textbox.ID = "testing"
        PlaceHolder.Controls.Add(textbox)

        Dim val As New RequiredFieldValidator
        val.ControlToValidate = textbox.ID
        val.Text = "required"
        val.EnableClientScript = True
        PlaceHolder.Controls.Add(val)

    End Sub
End Class

When you hit next, client side validation never fires. What's really weird is that when you wrap the button inside another update panel, the validation fires (in IE and Firefox, but not in Chrome or Safari).

Anyone have any ideas what's going on? I know that the first versions of Asp.net AJAX didnt support the validation controls, but everything is up to date on my end.

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

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

发布评论

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

评论(1

平安喜乐 2024-08-06 00:06:59

我看到有两个问题

  1. 当更新面板导致异步回发到服务器时,它无法使用动态控件创建控件树 - 因此请检查您是否从 Page_Load 为 ScriptManager.IsInAsyncPostBack == true 调用 RenderPage
  2. 更新面板下存在使用验证器问题 -必须在更新面板工作之前加载脚本。 我可以建议您在 UpdatePanel 下分配虚构的RequiredFieldValidator。 将它们设置为 Display=none (但不是 Visible=false !!!)或放置到不存在的 ValidationGroup。 这允许将 JScript 渲染到您的页面中。

I see there 2 problems

  1. When update panel causes async post back to server it cannot create tree of controls with your dynamic controls - so check that you call RenderPage from Page_Load for ScriptManager.IsInAsyncPostBack == true
  2. There is issue of usage validators under update panel - the scripts must be loaded before update panel works. I can propose you to allocate fictive RequiredFieldValidator under UpdatePanel. Set them Display=none (but not Visible=false !!!) or place to nonexistence ValidationGroup. This allows to render JScript into you page.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文