专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Silverlight

关于数据验证的有关问题

发布时间:2011-06-27 20:15:54 文章来源:www.iduyao.cn 采编人员:星星草
关于数据验证的问题!
LINQ后台模型代码,对于字段,我加上了[Required(ErrorMessage="必填!")]
并且在Set里加上了验证Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "CarNo" });

在XAML文件里的Textbox如下:
<TextBox Height="35" Text="{Binding CarNo,Mode=TwoWay,ValidatesOnExceptions=True, NotifyOnValidationError=True}" Name="textBox1" Width="137" />

后台代码:
C# code

        public Home()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ValidationSummary_Loaded);

        }


        void ValidationSummary_Loaded(object sender, RoutedEventArgs e)
        {
            WebServiceSoapClient client = new WebServiceSoapClient();

            client.GetBAS_CarListCompleted += new EventHandler<GetBAS_CarListCompletedEventArgs>(GetBAS_CarListCompleted);
            client.GetBAS_CarListAsync();
        }

        void GetBAS_CarListCompleted(object sender,GetBAS_CarListCompletedEventArgs e)
        {
            this.LayoutRoot.DataContext = e.Result;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            BindingExpression BE = this.textBox1.GetBindingExpression(TextBox.TextProperty);
            BE.UpdateSource();
            WebServiceSoapClient client = new WebServiceSoapClient();
            client.AddBAS_CarAsync(this.textBox1.Text, 1, "123", "123", 12, 12, 12, 12, 12, "123", "123213", 1, System.DateTime.Now);
        }



在Debug模式的时候,会弹出"必填"的错误提示,但是程序运行的时候,Textbox为空,点击插入按钮,则没有任何验证提示!哪里不对呢?
 

------解决方案--------------------
1. 在Debug模式的时候,会弹出"必填"的错误提示,这个现像是可以设置的。
应当是你在 Debug 选项中没有选择“仅我的代码”选项,并且在实体class上没有[System.Diagnostics.DebuggerStepThroughAttribute()]标记,此时Validator.ValidateProperty()引发异常时VS2008就会自动中断,但是在Release 时不会这样。

在添加服务引用时自动生成的实体class都会有 [System.Diagnostics.DebuggerStepThroughAttribute()],如果在Debug时不选择“仅我的代码”,设置在实体class中的断点是不会命中的。 

这个DebuggerStepThroughAttribute在 Release 的代码中无效。

2. UpdateSource() 本身是没有返回值的,无法直接根据 UpdateSource() 的结果来判断是否还要继续往下执行,一般应当先在上级容器中放置一个ValidationSummary ,通过
C# code

if (this.validationSummary1.HasErrors)
{
    // 停止执行
}
else
{
    // 继续执行
}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: