Is it possible to stop ObjectDataSource from auto-binding?
Asked Answered
A

3

7

I have a GridView to which I've using an ObjectDataSoure as the data source. The ObjectDataSource is taking in parameters from a TextBox and DropDownList which is then passed into the stored procedure. There is also a button called Search which can be used to force a refresh on the GridView by providing/changing values in the TextBox and/or DropDownList. However I noticed that if I changed the values I don't have to click on the Search button; simply clicking on the GridView causes a data bind.

Is there anyway to prevent this action while still using the ObjectDataSource?

Alchemy answered 9/8, 2012 at 4:12 Comment(3)
Please show your code.I think you have auto postback true for one of your control.Interesting
@ShoaibMuhammadKhan Neither the ObjectDataSource nor GridView control has an AutoPostback property. The TextBox and DropDownList has AutoPostback set as false.Alchemy
Possible duplicate : https://mcmap.net/q/1481557/-databind-and-postback #1262720Interesting
S
6

When you assign a DataSourceID on the GridView, the grid will automatically bind to the ObjectDataSource. You can simply omit that property on the GridView and wait until the Search button's click event to assign it.

Seale answered 9/8, 2012 at 4:21 Comment(6)
but how automatically page postback on gridview click?Interesting
@derek-hunziker Do you mean to remove the GridView's DataSourceID property from the aspx and set it from the code-behind?Alchemy
That's correct, remove it from the aspx page and wait until your click event to assign it in the codebehind. It would help to see your GridView definition as well as your DataSource. Can you post some code?Seale
@derek-hunziker The issue with that approach is I need the GridView to be bound at page load too. If I set the DataSourceID at page load (i.e. !Page.IsPostback) I'm still facing the same issue. I also tried to set the DataSource property, instead of the DataSourceID, and then manually binding it but I'd have to work manually code in the paging too then which defeats the purposed using the ObjectDataSource control.Alchemy
As for posting the code I'll see what I could do since it's against regulationsAlchemy
If the GridView Visible = false, it also will not bind the DataSource.Nisa
S
3

The problem is that every time any parameter used for ObjectDataSource is changed the ODS performs a "DataBind".

You can use two HiddenFields to keep the values. The ObjectDataSource will only do a "DataBind" when you change the values on the HiddenFields. So you can change the values on the TextBox and DropDownList, and when you want a "DataBind" you only need to copy the values to the HiddenFields.

Here is a code sample I made for another question: Q11874496WebApp.7z

Syllogistic answered 9/8, 2012 at 4:30 Comment(1)
Thanks @Syllogistic for your suggestion. I'm feeling slightly reluctant to use anymore hidden fields since I've already used quite a bit. However if bad comes to worse I'll probably use this, afterall one could argue how much of a difference is an additional couple of hidden fields going to make... right?Alchemy
I
1

In my case i just used a private boolean field in codebehind and respect its values on datasourceName_Selecting event.

For example i declared the following:

private bool IsInSearchingMode = false; 

set it true only in search mode:

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.IsInSearchingMode = true;
        this.gridData.DataBind();
    }

and then check the value on Selecting event:

        protected void myLinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            e.Result = new List<BranchDataClass>();
            if (!this.IsInSearchingMode)
                return; 

// e.result = select code 
}

A drawback is that a new page_load that is not caused by btnSearch_Click will reset the private variable's value. If you want it to be persistent you should use a hidden field as proposed or save it to viewstate.

Interjoin answered 21/2, 2014 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.