开发者

Search on linqDataSource requires 2 clicks sometimes

开发者 https://www.devze.com 2023-04-03 14:23 出处:网络
I have a page with linqdatasource and a gridView. There are several textBoxes to enter search conditions.

I have a page with linqdatasource and a gridView. There are several textBoxes to enter search conditions. LinqDataSource_OnSelecting event filters the query by something like this:

var query = from d in db.PersonData 
            select d;

if (textBoxName.Text != "")
    var query = query.where(p => p == textBoxName.Text); 

if (textBoxPhone.Text != "")
    var query = query.where(p => p == textBoxPhone.Text); 

e.Result = query开发者_JS百科;

And btnSearch_Click event has the following code:

gridView.DataBind();

It works pretty good most times, but sometimes I have to click search button twice to see the results. First time I click search, gridView is empty! Second time I have correct results. Hope you will clarify why it is sometimes goes that way. Thanks!


if you binding Gridview-Datasource manually and not use dataset you must always bind Gridview-Datasource in page load.

In web (asp.net) it's not like Win-app, you can't bind Datasource to grid without Page-Load event.

As solution: you can use Updatepanle and put Your Grid in that. and use GridName.DataBind() after binding.


The most likely reason you're getting blanks only, sometimes, is that you are on a page beyond which the search will yield results.

E.g.: you're on page 10 of the unfiltered results, hit Search, and get only 5 pages' worth of results. Thus, the page you're currently viewing (10) is blank.

After the LINQ statement has completed, you need to reset the GridView back to the first page.

A second (possible) problem is that you cannot guarantee the order of all event handlers. (See Page Life Cycle.) I.e., you cannot count on either LinqDataSource_OnSelecting or btnSearch_Click firing first, and you have the DataBind in the Click handler. Eliminate the btnSearch_Click event handler (or leave it blank) and use LinqDataSource.OnSelected, instead, which will definitely fire after LinqDataSource.OnSelecting:

protected void LinqDataSource_Selected(object sender, LinqDataSourceStatusEventArgs e) {
  gridView.DataBind();
  gridView.PageIndex = 0;  // back to beginning when searching
}
0

精彩评论

暂无评论...
验证码 换一张
取 消