C# ObjectDataSource、CustomValidator和数据绑定

C# ObjectDataSource、CustomValidator和数据绑定,c#,asp.net,C#,Asp.net,我有一个绑定到GridView的ObjectDataSource。对象接受文本框中的参数。我遇到的问题是,当我将CustomerValidator与ServerValidate事件一起使用时,ObjectDataSource仍然会尝试执行数据绑定,尽管客户验证程序返回false 以下是ASPX页面中的代码 <asp:TextBox ID="sittingDate" runat="server" /> <asp:CustomValidator ID="DateValidator"

我有一个绑定到GridView的ObjectDataSource。对象接受文本框中的参数。我遇到的问题是,当我将CustomerValidator与ServerValidate事件一起使用时,ObjectDataSource仍然会尝试执行数据绑定,尽管客户验证程序返回false

以下是ASPX页面中的代码

<asp:TextBox ID="sittingDate" runat="server" />
<asp:CustomValidator ID="DateValidator" runat="server" ControlToValidate="sittingDate" OnServerValidate="DateValidator_ServerValidate" />
<asp:ObjectDataSource ID="BatchDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
        SelectMethod="GetOrCreateSittingBatch" TypeName="BatchBLL" OnSelected="BatchDataSource_Selected" OnSelecting="BatchDataSource_Selecting">
        <SelectParameters>
            <asp:ControlParameter ControlID="sittingDate" Name="batchDate" PropertyName="Text"
                Type="DateTime" />
        </SelectParameters>
    </asp:ObjectDataSource>
<asp:GridView ID="BatchGridView" runat="server" DataSourceID="BatchDataSource">

验证失败时,如何停止ObjectDataSource的数据绑定?

尝试执行Page.Validate,然后检查Page.IsValid是否有效,以防止数据绑定,如:

this.Page.Validate();
if (this.Page.IsValid)
{
   ...
}

如果Page.IsValid为false,您可以将其添加到
Page\u Load
事件或ObjectDataSource的
OnDataBinding
事件中,以防止数据绑定。

感谢这一帮助。但是,在我的情况下,因为我正在执行类型转换检查,所以我需要将select参数从修改为,否则当输入无效数据时,在调用BatchDataSource_Selecting事件之前会引发类型异常。ObjectDataSource的数据绑定事件中如何防止数据绑定?只有没有.Cancel属性的EventArgs。
void BatchDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    if(!Page.IsValid)
        e.Cancel = true;
}
void BatchDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    if(!Page.IsValid)
        e.Cancel = true;
}