欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » AspxGridView使用教程 » 正文

最新评论

AspxGridView之数据校验

创建时间:2011年12月11日 22:35  阅读次数:(8440)
分享到:

行数据校验(编辑状态)
调用DoRowValidation()可以进行行数据校验。它实际上是触发RowValidating事件编写代码实现数据校验的。DoRowValidation方法对浏览状态的行无效,可以通过获取AspxGridView的IsEditing属性判断是否进入编辑状态。

RowValidating事件原型:
void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)

DevExpress.Web.Data.ASPxDataValidationEventArgs构造函数:
public ASPxDataValidationEventArgs(bool isNew);

DevExpress.Web.Data.ASPxDataValidationEventArgs属性:
Errors: Dictionary<GridViewColumn, string >,行错误集合,只读。
HasErrors:bool,待处理的行是否有错。只读。
IsNewRow:bool,待处理的行是否新增行。只读。
Keys:OrderedDictionary,要删除行的主键栏位值。只读。
NewValues: OrderedDictionary,要删除行的非主键(实际上是所有栏位)栏位新值。只读。
OldValues: OrderedDictionary,要删除行的非主键(实际上是所有栏位)栏位原值。只读。
RowError:string,行错误信息。可读写。

通常在RowValidating事件中检查栏位值是否有效,如果无效,应抛出异常。
例:
//行数据检验事件
protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
{    
    //是否新增行
    if (e.IsNewRow)
    {
        if (e.NewValues["Event"] == null)
        {                    
            e.Errors.Add(this.ASPxGridView1.Columns["Event"], "事件名称不可为空");
            e.RowError = "事件名称不可为空";
            throw new Exception("事件名称不可为空");

        }
    }
}

通常在HtmlRowPrepared事件中检查行是否有效并以不同的颜色来显示未通过校验的行。
通常在StartRowEditing事件中调用DoRowValidation方法触发数据校验。

以下是官方的示例代码:
using DevExpress.Web.ASPxGridView;
using System.Collections.Generic;

protected void grid_RowValidating(object sender, 
DevExpress.Web.Data.ASPxDataValidationEventArgs e) {
    // Checks for null values.
    foreach (GridViewColumn column in grid.Columns) {
        GridViewDataColumn dataColumn = column as GridViewDataColumn;
        if (dataColumn == null) continue;
        if (e.NewValues[dataColumn.FieldName] == null)
            e.Errors[dataColumn] = "Value cannot be null.";
    }
    // Displays the error row if there is at least one error.
    if (e.Errors.Count  > 0) e.RowError = "Please, fill all fields.";
    
    if (e.NewValues["ContactName"] != null && 
        e.NewValues["ContactName"].ToString().Length < 2) {
        AddError(e.Errors, grid.Columns["ContactName"], 
        "Contact Name must be at least two characters long.");
    }
    if (e.NewValues["CompanyName"] != null && 
    e.NewValues["CompanyName"].ToString().Length < 2) {
        AddError(e.Errors, grid.Columns["CompanyName"], 
        "Company Name must be at least two characters long.");
    }
    if (string.IsNullOrEmpty(e.RowError) && e.Errors.Count  > 0) 
    e.RowError = "Please, correct all errors.";
}

void AddError(Dictionary<GridViewColumn, string > errors, 
GridViewColumn column, string errorText) {
     if(errors.ContainsKey(column)) return;
     errors[column] = errorText;
 }

protected void grid_HtmlRowPrepared(object sender, 
ASPxGridViewTableRowEventArgs e) {
    // Checks whether the generated row has the errors.
    bool hasError = e.GetValue("ContactName").ToString().Length <= 1;
    hasError = hasError || e.GetValue("CompanyName").ToString().Length <= 1;
    hasError = hasError || e.GetValue("Country") == null;
    // If the row has the error(s), its text color is set to red.
    if (hasError)
        e.Row.ForeColor = System.Drawing.Color.Red;
}

protected void grid_StartRowEditing(object sender, 
DevExpress.Web.Data.ASPxStartRowEditingEventArgs e) {
    // Validates the edited row if it isn't a new row,.
    if (!grid.IsNewRowEditing)
        grid.DoRowValidation();
}
来源:.net学习网
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf】

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论17条
  • #1楼  评论人:匿名  评论时间:2024-10-9 20:16:49
  • I saw that your lmwlove.com website may be missing out on approximately a thousand visitors daily. Our AI powered traffic system is tailored to increase your site's visibility: https://ln.run/_s3yp
    We're offering a free trial that includes four thousand targeted visitors to show the potential benefits. After the trial, we can supply up to 250,000 targeted visitors per month. This solution could greatly enhance your website's reach and traffic.
  • #2楼  评论人:匿名  评论时间:2024-10-14 12:27:30
  • I see that your lmwlove.com website may be missing out on approximately a thousand visitors daily. Our AI powered traffic system is tailored to boost your site's visibility: https://ln.run/_s3yp
    We're offering a free trial that includes four thousand targeted visitors to show the potential benefits. After the trial, we can supply up to 250K targeted visitors per month. This solution could greatly increase your website's reach and traffic.
  • #3楼  评论人:匿名  评论时间:2024-10-19 19:45:49
  • I see that your lmwlove.com website might be missing out on approximately a thousand visitors daily. Our AI powered traffic system is tailored to boost your site's visibility: https://ln.run/VZn5V
    We're offering a free trial that includes four thousand targeted visitors to show the potential benefits. After the trial, we can supply up to 250K targeted visitors per month. This solution could greatly increase your website's reach and visitors.
  • #4楼  评论人:匿名  评论时间:2024-10-20 6:21:56
  • I saw that your lmwlove.com website might be missing out on approximately 1K visitors daily. Our AI powered traffic system is tailored to increase your site's visibility: https://ln.run/kCF8b
    We're offering a free trial that includes 4,000 targeted visitors to show the potential benefits. After the trial, we can supply up to a quarter million targeted visitors per month. This service could greatly enhance your website's reach and traffic.
  • #5楼  评论人:匿名  评论时间:2024-10-26 0:04:32
  • I see that your lmwlove.com website might be missing out on approximately a thousand visitors daily. Our AI powered traffic system is tailored to increase your site's visibility: https://cutt.ly/8eDg5TMA
    We're offering a free trial that includes 4,000 targeted visitors to show the potential benefits. After the trial, we can supply up to 250,000 targeted visitors per month. This service could greatly increase your website's reach and visitors.
  • #6楼  评论人:匿名  评论时间:2024-10-26 22:54:09
  • I see that your lmwlove.com website may be missing out on approximately a thousand visitors daily. Our AI powered traffic system is tailored to increase your site's visibility: https://ow.ly/QRAX50TR2rR
    We're offering a free trial that includes 4,000 targeted visitors to show the potential benefits. After the trial, we can supply up to 250,000 targeted visitors per month. This solution could greatly increase your website's reach and engagement.
  • #7楼  评论人:匿名  评论时间:2024-10-29 23:48:25
  • I saw that your lmwlove.com website could be missing out on approximately 1K visitors daily. Our AI powered traffic system is tailored to increase your site's visibility: https://cutt.ly/8eDg5TMA
    We're offering a free trial that includes four thousand targeted visitors to show the potential benefits. After the trial, we can supply up to 250,000 targeted visitors per month. This opportunity could greatly increase your website's reach and visitors.
  • #8楼  评论人:匿名  评论时间:2024-10-31 6:48:41
  • I see that your lmwlove.com website may be missing out on approximately 1,000 visitors daily. Our AI powered traffic system is tailored to increase your site's visibility: https://ow.ly/82vQ50TR2qR
    We're offering a free trial that includes 4,000 targeted visitors to show the potential benefits. After the trial, we can supply up to 250,000 targeted visitors per month. This opportunity could greatly enhance your website's reach and engagement.
  • #9楼  评论人:匿名  评论时间:2024-11-5 3:27:32
  • I noticed that your lmwlove.com website may be missing out on approximately 1,000 visitors daily. Our AI powered traffic system is tailored to boost your site's visibility: https://cutt.ly/8eDg5TMA
    We're offering a free trial that includes 4,000 targeted visitors to show the potential benefits. After the trial, we can supply up to 250,000 targeted visitors per month. This solution could greatly amplify your website's reach and engagement.
  • #10楼  评论人:匿名  评论时间:2024-11-6 1:08:21
  • I see that your lmwlove.com website might be missing out on approximately a thousand visitors daily. Our AI powered traffic system is tailored to boost your site's visibility: https://ow.ly/82vQ50TR2qR
    We're offering a free trial that includes four thousand targeted visitors to show the potential benefits. After the trial, we can supply up to 250,000 targeted visitors per month. This service could greatly enhance your website's reach and engagement.
  • #11楼  评论人:匿名  评论时间:2024-11-6 21:00:14
  • I saw that your lmwlove.com website could be missing out on approximately 1,000 visitors daily. Our AI powered traffic system is tailored to enhance your site's visibility: https://ow.ly/T4ag50TR2oM
    We're offering a free trial that includes four thousand targeted visitors to show the potential benefits. After the trial, we can supply up to a quarter million targeted visitors per month. This solution could greatly amplify your website's reach and traffic.
  • #12楼  评论人:匿名  评论时间:2024-11-9 12:54:55
  • I saw that your lmwlove.com website may be missing out on approximately 1,000 visitors daily. Our AI powered traffic system is tailored to enhance your site's visibility: https://ow.ly/mtnY50U3i70
    We're offering a free trial that includes 4,000 targeted visitors to show the potential benefits. After the trial, we can supply up to a quarter million targeted visitors per month. This opportunity could greatly enhance your website's reach and engagement.
  • #13楼  评论人:匿名  评论时间:2024-11-11 14:13:06
  • I see that your lmwlove.com website could be missing out on approximately 1,000 visitors daily. Our AI powered traffic system is tailored to boost your site's visibility: https://ow.ly/mtnY50U3i70
    We're offering a free trial that includes 4,000 targeted visitors to show the potential benefits. After the trial, we can supply up to 250K targeted visitors per month. This solution could greatly increase your website's reach and engagement.
  • #14楼  评论人:匿名  评论时间:2024-11-14 5:20:36
  • I noticed that your lmwlove.com website could be missing out on approximately 1,000 visitors daily. Our AI powered traffic system is tailored to enhance your site's visibility: https://ow.ly/b6IF50U3its
    We're offering a free trial that includes four thousand targeted visitors to show the potential benefits. After the trial, we can supply up to 250K targeted visitors per month. This service could greatly enhance your website's reach and traffic.
  • #15楼  评论人:匿名  评论时间:2024-11-14 14:46:50
  • I saw that your lmwlove.com website could be missing out on approximately a thousand visitors daily. Our AI powered traffic system is tailored to boost your site's visibility: https://ow.ly/BkSO50U3isZ
    We're offering a free trial that includes four thousand targeted visitors to show the potential benefits. After the trial, we can supply up to 250K targeted visitors per month. This solution could greatly amplify your website's reach and visitors.
  • #16楼  评论人:匿名  评论时间:2024-11-21 9:18:50
  • I see that your lmwlove.com website might be missing out on approximately 1K visitors daily. Our AI powered traffic system is tailored to increase your site's visibility: https://ow.ly/V0j150Uaobz
    We're offering a free trial that includes four thousand targeted visitors to show the potential benefits. After the trial, we can supply up to 250K targeted visitors per month. This opportunity could greatly increase your website's reach and visitors.
  • #17楼  评论人:匿名  评论时间:2024-11-23 4:56:42
  • I saw that your lmwlove.com website might be missing out on approximately 1K visitors daily. Our AI powered traffic system is tailored to boost your site's visibility: https://ow.ly/tLXG50UaobP
    We're offering a free trial that includes four thousand targeted visitors to show the potential benefits. After the trial, we can supply up to 250,000 targeted visitors per month. This service could greatly amplify your website's reach and engagement.
发表评论:
留言人:
内  容:
请输入问题 72+60=? 的结果(结果是:132)
结  果: