Home All Groups Group Topic Archive Search About

fill ObjectDatasource on request

Author
5 Sep 2006 10:48 AM
HIK
I have an asp.net 2.0 form with an combo box and an objectdatasource and
a gridview bound to the objectdatsource.  I would like to fill the
objectdatasource only when the user makes a selection from the combobox.

By default the objectdatasource retrieves the data when the page loads.
   How can I prevent it from retrieving data when the page loads and
only on postbacks that the user controls.

Haim

Author
6 Sep 2006 2:42 AM
Walter Wang [MSFT]
Hi Haim,

From your post, I understand that you're binding a GridView to an
ObjectDataSource which has a ControlParameter associated to a
DropDownList.SelectValue. And you don't want the ObjectDataSource to
retrieve data on first page load. Please feel free to post here if I've
misunderstood anything.

First, if you are using declarative syntax to configure the
ObjectDataSource and associate it with the DropDownList, I don't think you
can prevent it from reading data the first time. As far as I know, there's
no event in the ObjectDataSource that you can cancel the data binding.
However, you can prevent the GridView to bind to the ObjectDataSource the
first time by:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSourceID = "";
        }
        else
        {
            GridView1.DataSourceID = "ObjectDataSource2";
        }
    }

The ObjectDataSource will still read the data but the result is not shown
in the GridView on the first page load.

If you need to completely prevent the ObjectDataSource from loading data on
first page load, you have to construct the ObjectDataSource dynamically. I
will depict more on this if you need information on this.


Sincerely,
Walter Wang (waw***@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Author
6 Sep 2006 6:48 AM
HIK
Thank you Walter. You understood my scenario completely.  That is
exactly the information I need to solve my problem.

haim

Walter Wang [MSFT] wrote:

Show quoteHide quote
> Hi Haim,
>
> From your post, I understand that you're binding a GridView to an
> ObjectDataSource which has a ControlParameter associated to a
> DropDownList.SelectValue. And you don't want the ObjectDataSource to
> retrieve data on first page load. Please feel free to post here if I've
> misunderstood anything.
>
> First, if you are using declarative syntax to configure the
> ObjectDataSource and associate it with the DropDownList, I don't think you
> can prevent it from reading data the first time. As far as I know, there's
> no event in the ObjectDataSource that you can cancel the data binding.
> However, you can prevent the GridView to bind to the ObjectDataSource the
> first time by:
>
>     protected void Page_Load(object sender, EventArgs e)
>     {
>         if (!IsPostBack)
>         {
>             GridView1.DataSourceID = "";
>         }
>         else
>         {
>             GridView1.DataSourceID = "ObjectDataSource2";
>         }
>     }
>
> The ObjectDataSource will still read the data but the result is not shown
> in the GridView on the first page load.
>
> If you need to completely prevent the ObjectDataSource from loading data on
> first page load, you have to construct the ObjectDataSource dynamically. I
> will depict more on this if you need information on this.
>
>
> Sincerely,
> Walter Wang (waw***@online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
> ications.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/default.aspx.
> ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
Author
6 Sep 2006 7:44 AM
Piotrek
Hi.

There is also another solution for your problem (only if you are
writing your custom BLL classes):
1. Add new parameter - bool performSearch - to your 'Select' method
from BLL.
2. Check value of this parameter in 'Select' method - if it is false,
then do not query DB.
3. By default set value of this parameter to false.
4. Set AutoPostback property of DropDownList to true and set
performSearch parameter to true in its SelectionChanged event.
5. In ODS.Selecting event pass proper value of performSearch to your
'Select' method.
6. In.ODS.Selected event remeber to set value of performSearch param to
false.

That's all.

I know it looks a bit complicated, but it works and you have completely
control of querying DB.

Piotrek
Author
30 Nov 2006 4:32 PM
J055
What about this?

protected void ods_Selecting(object sender,
ObjectDataSourceSelectingEventArgs e)
{
    if (!Page.IsPostBack)
    e.Cancel = true;
}

Andrew