Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObjectDataSource, CustomValidators and DataBinding

Tags:

c#

asp.net

I have an ObjectDataSource that is bound to a GridView. The object accepts a parameter from a TextBox. The problem I have is when I use a CustomerValidator with a ServerValidate event, the ObjectDataSource would still attempt to perform a DataBind despite the fact the the customer validator has returned false.

The following is the code in the ASPX page.

<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">

In the Custom Validator I have

protected void DateValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
        //Ensure that the entered data is a date.
        string input = args.Value;

        DateTime result;
        args.IsValid = DateTime.TryParse(input.TrimEnd(), out result);
}

How do I stop the ObjectDataSource from data binding when validation has failed?

like image 385
Hidden_au Avatar asked May 29 '26 02:05

Hidden_au


1 Answers

void BatchDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    if(!Page.IsValid)
        e.Cancel = true;
}
like image 68
Bryan Avatar answered May 31 '26 14:05

Bryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!