开发者

Prevent LinqDataSource selecting on every post-back

开发者 https://www.devze.com 2023-03-18 08:49 出处:网络
There are a GridView and a LinqDataSource in a page and few buttons which their actions are not related to the GridView and its LinqDataSource. Why on each post-back of those buttons the Selecting met

There are a GridView and a LinqDataSource in a page and few buttons which their actions are not related to the GridView and its LinqDataSource. Why on each post-back of those buttons the Selecting method of the LinqDataSource will call? Is this normal?! These unwanted db calls from t开发者_StackOverflow中文版he LinqDataSource are not required.

Is there any better way?


You need to detach the GridView from the data source. I assume you have attached the data source like this, in which case, don't do it this way.

<asp:LinqDataSource 
    runat="server"
    ContextTypeName="AdventureWorksDataContext" 
    TableName="Contacts" 
    ID="LinqDataSource1">
</asp:LinqDataSource>

<asp:GridView 
    ID="GridView1" 
    runat="server"
    DataSourceID="LinqDataSource1" >
</asp:GridView>

Your better off attaching the data source in your code behind when it's needed.

if (dataSourceNeeded == true) {
  GridView1.DataSource = GetDataSource();
  GridView1.DataBind();
}


it because grid need to populated on every page load, you may cache datasorce to some variable and store it on the server side (not in viewstate)

0

精彩评论

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

关注公众号