fill ObjectDatasource on request

H

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
 
W

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 ([email protected], 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.
 
H

HIK

Thank you Walter. You understood my scenario completely. That is
exactly the information I need to solve my problem.

haim
 
P

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
 
J

J055

What about this?

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

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top