简体   繁体   English

asp.net webforms中的错误集合

[英]error collection in asp.net webforms

I am trying to change the css of a textbox based on an error on the page. 我试图根据页面上的错误更改文本框的CSS。 Say to turn the background of the textbox red. 说要将文本框的背景变为红色。 I want to do this through the base page so that each codebehind that inherits this base page will perform this function. 我想通过基页执行此操作,以便继承此基页的每个代码隐藏都将执行此功能。 I am trying to do this in the OnLoad event 我试图在OnLoad事件中这样做

protected override void OnLoad(EventArgs e)
{
    //code here
    base.OnLoad(e);
}

How do I access the error collection in the base page something like this... 如何访问基本页面中的错误集合...

for each(var error in Page.Errors)
{
    TextBox textBox = error.textboxInError;
    textBox.Background - Color = "Red";
}

To be more specific I want to trigger on page validation errors. 更具体地说,我想触发页面验证错误。

If you're using web forms validators, you could do something like this: 如果您使用的是Web表单验证程序,则可以执行以下操作:

// Get a collection of all validators to check, sort of like this
var allValidators = new[] { validator1, validator2, validator3 };

foreach (var validator in allValidators)
{
    validator.Validate();
    var ctrl = (WebControl)Page.FindControl(validator.ControlToValidate);
    ctrl.BackColor = validator.IsValid ? Colors.Red : Colors.White;
}

Update 更新

Apparently, the Page object has a collection of validators. 显然, Page对象有一组验证器。 See Page.Validators . Page.Validators Here's some revised code using that: 以下是使用该修改后的代码:

foreach (var validator in Page.Validators)
{
    validator.Validate();
    var ctrl = (WebControl)Page.FindControl(validator.ControlToValidate);
    ctrl.BackColor = validator.IsValid ? Colors.Red : Colors.White;
}

Check This tutorial Out . 查看本教程 It will help you create a Custom Error Page, and trap the error at either Application, Page or Web.Config level. 它将帮助您创建自定义错误页面,并在Application,Page或Web.Config级别捕获错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM