GridView 中的 ASP.Net CustomValidator 未触发

发布于 2024-08-05 01:52:10 字数 1042 浏览 8 评论 0原文

我在 UpdatePanel 中得到了一个带有此 EditTemplate 的 Gridview:

<edititemtemplate>
    <asp:textbox id="txtDistFrom" runat="server" text='<%# Bind("distFrom") %>' width="30" />
    <asp:CustomValidator ID="valDistFrom" ValidateEmptyText="True" OnServerValidate="valDistFromTo_ServerValidate" ControlToValidate="txtDistFrom" Text="Missing" ToolTip="Invalid" Display="Dynamic" runat="server" />
</edititemtemplate>

和一个简单的服务器端函数:

Protected Sub valDistFromTo_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
    Dim cv As CustomValidator = CType(source, CustomValidator)
    Dim gvr As GridViewRow = cv.NamingContainer
    Dim tbV As UI.WebControls.TextBox = gvr.FindControl("txtDistFrom")
    If tbV.Text <> "" Then
        args.IsValid = False
        cv.ErrorMessage = "inhalt ist " & tbV.Text
    End If
End Sub

但是在调试此代码时,无论它做什么,服务器端函数都不会被触发。看来它与 gridview 有关,所以我无法直接通过其 id 访问该控件。有什么建议吗?

I got a Gridview in an UpdatePanel with this EditTemplate:

<edititemtemplate>
    <asp:textbox id="txtDistFrom" runat="server" text='<%# Bind("distFrom") %>' width="30" />
    <asp:CustomValidator ID="valDistFrom" ValidateEmptyText="True" OnServerValidate="valDistFromTo_ServerValidate" ControlToValidate="txtDistFrom" Text="Missing" ToolTip="Invalid" Display="Dynamic" runat="server" />
</edititemtemplate>

And a simple Server-side function:

Protected Sub valDistFromTo_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
    Dim cv As CustomValidator = CType(source, CustomValidator)
    Dim gvr As GridViewRow = cv.NamingContainer
    Dim tbV As UI.WebControls.TextBox = gvr.FindControl("txtDistFrom")
    If tbV.Text <> "" Then
        args.IsValid = False
        cv.ErrorMessage = "inhalt ist " & tbV.Text
    End If
End Sub

But when debugging this code the server-side function is not fired, whatever it does. It seems it has to do with the gridview, so I cannot access the control directly by its id. Any suggestions?

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

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

发布评论

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

评论(5

瀞厅☆埖开 2024-08-12 01:52:10

如果您将 VB 修改为:

Protected Sub valDistFromTo_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        Dim cv As CustomValidator = CType(source, CustomValidator)
        If args.Value <> "" Then
            args.IsValid = False
            cv.ErrorMessage = "inhalt ist " & args.Value
        End If
End Sub

它应该可以工作。请注意,我使用的是 args.Value。我在 EditTemplates 中使用 CustomValidators 和 TextBox,并将 ControlToValidate 设置为 TextBox ID ,并且它可以正常工作,只是无法按照您尝试的方式获取 TextBox 对象。我认为这比 TGnat 的回答中建议的乱搞 RowUpdating 事件要少得多,而且要干净得多。

If you modify your VB to:

Protected Sub valDistFromTo_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        Dim cv As CustomValidator = CType(source, CustomValidator)
        If args.Value <> "" Then
            args.IsValid = False
            cv.ErrorMessage = "inhalt ist " & args.Value
        End If
End Sub

It should work. Note that I'm using args.Value. I use CustomValidators and TextBox within EditTemplates with ControlToValidate set to the TextBox ID all the time and it works, you just can't get the TextBox object the way you're trying it. I think this is far less of a pain and much cleaner than messing around with RowUpdating Event as suggested in TGnat's answer.

紫竹語嫣☆ 2024-08-12 01:52:10

在这种情况下,您可以使用必填字段验证器。这在网格中应该可以正常工作。

对于服务器端验证,我会将自定义验证器完全移到网格之外,并将 ControlToValidate 属性留空。您可以将验证移至网格的 RowUpdating 事件,并在自定义验证器上设置任何错误消息。请适当设置验证器 IsValid 属性。

In this case you can use a required field validator. Which should work just fine in a grid.

For server side validation I would move the custom validator outside the grid entirely and leave the ControlToValidate property blank. You can move your validation to the RowUpdating event of the grid and set any error messages on the custom validator. Rmember to set the validators IsValid property appropriately.

红颜悴 2024-08-12 01:52:10

该问题与 ControlToValidate 属性有关,因为文本框的 ID 不用于重复元素(如 GridView、ListView 和 Repeater)。换句话说:您偶然发现了 ASP.NET 引擎的限制。

但我不确定如何解决这个问题。您也许可以通过将一个方法附加到 GridView 的 OnRowBound 方法来以编程方式添加 CustomValidator。

本文可能提供答案 本文可能提供答案: Integrating Asp.Net Validation Controls with GridView at run-时间

The problem is related to the ControlToValidate property, because the ID of your text box is not used in repeating elements like GridView, ListView and Repeater. In other words: You have stumbled upon a limitation in ASP.NET's engine.

I am not sure how to solve this problem, though. You might be able do it, by adding the CustomValidator programmatically by attaching a method to the GridView's OnRowBound method.

This article might provide an answer This article might provide an answer: Integrating Asp.Net Validation Controls with GridView at run-time.

悍妇囚夫 2024-08-12 01:52:10

我也倾向于认为 ControlToValidate 是问题所在。 .NET 在运行时更改该控件的 ID,并且自定义验证器可能不会拾取它。

我会尝试使用 FindControl() 在 RowCreated 或 RowDatabound 上添加 customvalidator

I also tend to think that ControlToValidate is the problem. .NET changes the ID of that control at runtime and the custom validator probably isn't picking it up.

I would try adding the customvalidator on RowCreated or RowDatabound using the FindControl()

往昔成烟 2024-08-12 01:52:10

我也有同样的问题。当我在自定义验证器中显式设置此属性时,服务器端代码被触发:

    EnableClientScript="false"

I had the same problem. When I explicitly set this property in my customvalidator, the server side code fired:

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