继承自 asp:validationsummary
我需要在页面(或控件)顶部显示格式良好的错误消息。
因此我实现了新创建的服务器控件的渲染方法。 新创建的控件继承自ValidationSummary。
public class AgValidationSummary : ValidationSummary
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
if(this.Enabled)
{
if (Page.IsPostBack && !Page.IsValid)
{
我现在的问题是,如果一个按钮被触发并且他的属性 CausesValidation 设置为 false 我的validationsummary 将抛出异常,因为我询问 Page.IsValid 属性(并且只有在调用 Page.validate( 时才会设置此属性) )。
有人解决这个问题吗?
i have the requirement to show a nicely formatted error-message on top of the page (or control).
therefore i implemented the render method of a new created server control. the new created control inherits from ValidationSummary.
public class AgValidationSummary : ValidationSummary
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
if(this.Enabled)
{
if (Page.IsPostBack && !Page.IsValid)
{
my problem is now, that if an button is fired and his property CausesValidation is set to false my validationsummary will throw an exception because i ask about the Page.IsValid property (and this is only set if there was an call to Page.validate().
has somebody a solution for the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案很简单:
只是不要解析
Page.IsValid
标志:)而是在您的void Render(HtmlTextWriter writer)
中执行类似的操作:这样做的原因:
导致回发的控件已获取有关
的信息,因此 ASP.net-engine 本身将执行关联的验证器并将它们设置为
IsValid
或不设置。编辑
要删除原始的 HeaderText(仍然呈现):
the solution is somehow easy:
just don't parse the
Page.IsValid
flag :) instead do something like that in yourvoid Render(HtmlTextWriter writer)
:The reason why this works:
The control, which causes the postback, has got the information about
So the ASP.net-engine itself will execute the associated validators and set them to
IsValid
or not.edit
To get rid of the original HeaderText (which is still rendered):
也许你可以将 IsValid 属性保存在 ViewState 中,并将其初始化为 true。
在Load中检查IsValid是否为null,如果不为null,则将你ViewState中的IsValid设置为Page.IsValid中的值。
在 Render 中,从 ViewState 读取 IsValid。
Maybe you can save IsValid property in ViewState, and initialize it to true.
In Load, check if IsValid is null, if not null, set your IsValid in ViewState to the value in Page.IsValid.
And in Render, read IsValid from ViewState.
我可能偏离了轨道,但是您不能在控件的构造函数中将其连接到页面的 Validate 事件吗?
如果需要,您可以有一个内部标志,渲染器会检查该标志以查看是否执行渲染代码中接下来发生的任何操作。
I may be off track here, but can't you just wire up in your control's constructor to the page's Validate event?
If needed, you can have an internal flag that the render checks to see whether to do whatever happens next in your render code.