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?
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With