Question

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!

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top