验证器在复合控件内回发时消失
这应该是一个很容易解决的问题,因为它使用的方法与我解决 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经找到问题了。 显然,在 PostBack 上,无论您如何命令 Validator 的创建,其显示都会设置为 None。 我通过中断 Render 方法并检查 Validator 变量发现了这一点。
解决方案(hack?)是在 Render 方法期间将验证器设置为您想要的 Display。
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.