views:

268

answers:

1

I am using an AccessDataSource control bound to a DetailsView control. When the select returns no rows (the DetailsView disappears, but is still set to visible), then I would like to disable some further functionality by disabling a button.

In the C# code behind, I know I will need:

ButtonMyButton.enabled = false;

However, I am uncertain of a way of getting the information from the AccessDataSource or the DetailsView that would let me know when to set the value.

I have seen some other articles that suggested the example of for a similar item:

        DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        int reorderedProducts = (int)dv.Table.Rows[0][0]

For me, DataView is in error as not having the right namespace, despite having:

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

What should I do to disable the button?

(While this is a real newbie question, it is not school homework, since that seems to be the first comment on so many simple questions.)

+2  A: 

The AccessDataSource inherits from the SqlDataSource class. It therefore provides an event called Selected which is the event raised after the SELECT operation is completed. This event should have an EventArgs parameter of the type SqlDataSourceEventArgs).

This EventArgs parameter has a property called AffectedRows which will tell you the count of rows retrieved. If it is 0, you can set the enabled property of your Button.

private void OnSelectedHandler(Object source, SqlDataSourceStatusEventArgs e) 
{
  if (e.AffectedRows == 0)
    //Disable the button.
}

For the record, the DataView class resides in the System.Data namespace. In addition, calling Select() on the AccessDataSource will mean that you are running two select statements, one to retrieve the records (if any), and the other simply to count the number of retrieved records.

Cerebrus
This works perfectly for me. Thank you for taking the time to explain. I did have to change "private" to "protected" to work on my aspx.cs page.
Degan
You're most welcome and it's my pleasure. :-)
Cerebrus