验证器在复合控件内回发时消失

发布于 2024-07-26 20:31:58 字数 1000 浏览 3 评论 0原文

这应该是一个很容易解决的问题,因为它使用的方法与我解决 FooControl 的上一个问题(如下)相同。

基本上,我想向这个复合控件添加我制作的派生验证器。 它工作正常,但在回发时它就消失在标记中,让我认为它丢失了其 ViewState。

我可能在实例化它时做错了什么,但我尝试只设置 ControlToValidate,移动一些东西,但没有任何效果。

我提供了一些周围的代码来查看哪些有效,哪些无效。

    Private FooControl As IFooControl
    Private Validator As MyValidator

    Protected Overrides Sub CreateChildControls()

        FooControl = FooControlProvider.CreateFooControl(blah)

        Me.Controls.Add(FooControl.RetrieveControl())            

        ' Begin Not Working

        Validator = New MyValidator()
        Me.Controls.Add(Validator)

        Validator.ID = "MyValidatorID"
        Validator.ControlToValidate = FooControl.ID
        Validator.IsRequired = True ' Custom property
        Validator.ErrorMessage = "Please select an answer"

        ' End Not Working

        If Not DataSource Is Nothing Then
            FooControlProvider.AssignDataSource(DataSource, FooControl)
        End If
    End Sub

This should be a simple problem to fix, as it uses the same way I fixed my last problem with FooControl (below).

Basically, I want to add a derived validator I made to this composite control. It works fine but on postback it just disappears in the markup, making me think it's lost its ViewState.

I am probably doing something wrong with instantiating it, but I've tried setting only the ControlToValidate, moving things around, and nothing works.

I've provided some surrounding code to see what's working and then what's not.

    Private FooControl As IFooControl
    Private Validator As MyValidator

    Protected Overrides Sub CreateChildControls()

        FooControl = FooControlProvider.CreateFooControl(blah)

        Me.Controls.Add(FooControl.RetrieveControl())            

        ' Begin Not Working

        Validator = New MyValidator()
        Me.Controls.Add(Validator)

        Validator.ID = "MyValidatorID"
        Validator.ControlToValidate = FooControl.ID
        Validator.IsRequired = True ' Custom property
        Validator.ErrorMessage = "Please select an answer"

        ' End Not Working

        If Not DataSource Is Nothing Then
            FooControlProvider.AssignDataSource(DataSource, FooControl)
        End If
    End Sub

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

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

发布评论

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

评论(1

浊酒尽余欢 2024-08-02 20:31:58

我已经找到问题了。 显然,在 PostBack 上,无论您如何命令 Validator 的创建,其显示都会设置为 None。 我通过中断 Render 方法并检查 Validator 变量发现了这一点。

解决方案(hack?)是在 Render 方法期间将验证器设置为您想要的 Display。

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        Validator.Display = ValidatorDisplay.Dynamic
        MyBase.Render(writer)
    End Sub

I've found the problem. Apparently, on PostBack, no matter how you order the creation of the Validator, its display is set to None. I found this by breaking on the Render method and checking the Validator variable.

The solution (hack?) is to set the validator to your desired Display during the Render method.

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        Validator.Display = ValidatorDisplay.Dynamic
        MyBase.Render(writer)
    End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文