Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to stop ObjectDataSource from auto-binding?

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?

like image 438
Rusty Wizard Avatar asked Aug 09 '12 04:08

Rusty Wizard


3 Answers

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.

like image 152
Derek Hunziker Avatar answered Nov 12 '22 19:11

Derek Hunziker


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

like image 44
Hailton Avatar answered Nov 12 '22 18:11

Hailton


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.

like image 33
Nikos Tsokos Avatar answered Nov 12 '22 19:11

Nikos Tsokos