views:

468

answers:

1

On an ASP.NET web page I have an EntityDataSource:

<asp:EntityDataSource ID="EntityDataSourceOrders" runat="server" 
        ConnectionString="name=EntitiesContext"
        DefaultContainerName="EntitiesContext" 
        EntitySetName="Order" 
        Select="it.OrderID,it.OrderCode,it.OrderDateTime"
        Where="it.OrderDateTime &gt;= @DateTimeFrom AND it.OrderDateTime &lt;= @DateTimeTo"
        OrderBy="it.SOrderCode"
        StoreOriginalValuesInViewState="False" >
        <WhereParameters>
            <asp:ControlParameter Name="DateTimeFrom" 
                                  ControlID="TextBoxDateTimeFrom"
                                  DbType="DateTime"
                                  PropertyName="Text" />
            <asp:ControlParameter Name="DateTimeTo"
                                  ControlID="TextBoxDateTimeTo"
                                  DbType="DateTime"
                                  PropertyName="Text" />
        </WhereParameters>
</asp:EntityDataSource>

As you can see there are two Textboxes on the page to enter an earliest and latest date for my selection. These Textboxes are used as ControlParameters in Where-clause of the EntityDataSource.

Now imagine someone enters an invalid date like "32/01/2010" in one of those ControlParameter textboxes.

I am aware that I can validate at first on client side (using the ASP.NET validators), so I prevent a postback if the input is invalid.

But how do I implement the more important "final" validation on server side? Especially WHERE (which method or event) do I implement it to prevent the EntityDataSource to execute the query with invalid DateTime values in the Textboxes?

Basically my idea was to call something like Page.Validate() and then Page.IsValid and, if IsValid returns false, to "cancel" the EntityDataSource query execution (or prevent it to start at all). But I don't know where or in which event I can hook up into the EntityDataSource to prevent the query execution.

Perhaps I am thinking into the wrong direction. Does somebody have an idea what to do?

Thank you in advance for help!

+2  A: 

It's not the responsibility of the EntityDataSource to check for validity. When you have a 'save' button on the page, the code behind the button click can check for Page.IsValid. When you are working with standard ASP.NET controls, such as the GridView, those controls will call Page.IsValid for you.

So in your situation, I'd go with ASP.NET validation controls on the page. They will also run on the server side.

[Update]

You can hook-up a method to the EntityDataSource.Selecting event and set the Cancel property of the EntityDataSourceSelectingEventArgs to true when the page is not valid.

private void EntityDataSourceOrders_Selecting(
    object sender, EntityDataSourceSelectingEventArgs e)
{
    e.Cancel = !this.Page.IsValid;
}
Steven
@Steven: I understand your proposal. But my problem is that I don't know what to do if the validation fails. For example: In the ButtonClick event you mentioned I check `Page.IsValid` and it returns false. What now? I want to tell now the EntityDataSource NOT to run a query with those invalid dates. (Instead the user shall correct the dates at first.) The only way I can think of at the moment is to clear the TextBoxes. Otherwise if I don't do this, the ControlParameter collection of the EntityDataSource "automatically" pulls the invalid dates from the TextBoxes and throws an exception.
Slauma
I updated my answer. I think this ansers your question.
Steven
Great! That is exactly what I was looking for! And so easy, embarrassing that I didn't see that event. Thank you very much!
Slauma
Your welcome :-)
Steven