views:

23

answers:

1

Hello all.

I have the following bit of code that gives me the number of rows in a gridview bound to an object data source.

protected void odsProduct_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            lblHowManyRows.Text = ((List<tblProduct>)e.ReturnValue).Count.ToString();

Lovely.

However, how would I go about essentially doing the same count but before the object data source produces the gridview output?

What I am trying to do is give the user the chance to check how many rows would be returned before they actually create the gridview (bound to the object data source)

Is there something that lies within the ods or should I just write another linq statement and bind it to that?

Apologies for my ignorance

A: 

Well, you could have an OnSelecting event such as this:

protected void OnSelecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (lblHowManyRows.Text == string.Empty)
        {
            e.Cancel = true;
            lblHowManyRows.Text = [Linq statement here].Count().ToString();
        }
    }

where it sets the label to the count the first time, and retrieves the data the next time.

The next time the lblHowManyRows won't be empty, so it'll make it to your odsProduct_Selected method:

        protected void odsProduct_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        lblHowManyRows.Text = string.Empty;
    }

Blank out the label so that the user can continue to select. This way the first time they click they get only the count and the next time they click they get the full grid bound.

If it were me, I'd most likely retrieve the complete count via AJAX/webservice without doing a postback or using the gridview at all, but I don't know the full context of the question.

DougJones