views:

144

answers:

1

I have the following scenario.

A Page with a DetailsView binded to an ObjectDatasource with cache-enabled. The SelectMethod is assigned at Page_Load event, depending on my User Level Logic.

After assigned the selectMethod and Parameters for the ODS, if Cache not exists, then ODS will be cached the first time. The next time, the cache will be applied to the ODS and the select event don't need to be fired since the dataresult is cached.

The problem is, the ODS Cache works fine, but I have a Refresh button to clear the cache and rebind the DetailsView.

Am I doing correctly ? Below is my code.

<asp:DetailsView ID="DetailsView1" 
    runat="server" 
    DataSourceID="ObjectDataSource_Summary" 
    EnableModelValidation="True" 
    EnableViewState="False" 
    ForeColor="#333333" GridLines="None">
</asp:DetailsView>

<asp:ObjectDataSource ID="ObjectDataSource_Summary" 
    runat="server" 
    SelectMethod="" 
    TypeName="BL.BusinessLogic" 
    EnableCaching="true">
    <SelectParameters>
        <asp:Parameter Name="idCompany" Type="String" />
    <SelectParameters>
</asp:ObjectDataSource>
<asp:ImageButton ID="ImageButton_Refresh" runat="server" OnClick="RefreshClick" ImageUrl="~/img/refresh.png" />

And here is the code behind

public partial class Index : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

            ObjectDataSource_Summary.SelectMethod = "";
            ObjectDataSource_Summary.SelectParameters[0].DefaultValue = "";

            switch (this._loginData.UserLevel) //this is a struct I use for control permissions e pages behaviour
            {
                case OperNivel.SysAdmin:
                case OperNivel.SysOperator:
                    {
                        ObjectDataSource_Summary.SelectMethod = "SystemSummary";
                        ObjectDataSource_Summary.SelectParameters[0].DefaultValue = "0";
                        break;
                    }
                case OperNivel.CompanyAdmin:
                case OperNivel.CompanyOperator:
                    {
                        ObjectDataSource_Summary.SelectMethod = "CompanySummary";
                        ObjectDataSource_Summary.SelectParameters[0].DefaultValue = this._loginData.UserLevel.ToString();
                        break;
                    }
                default: break;
            }
    }
    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        if (Cache[ObjectDataSource_Summary.CacheKeyDependency] == null)
        {
            this._loginData.LoginDatetime = DateTime.Now;
            Session["loginData"] = _loginData;
            Cache[ObjectDataSource_Summary.CacheKeyDependency] = _loginData;
            DetailsView1.DataBind();
        }
    }

    protected void RefreshClick(object sender, ImageClickEventArgs e)
    {
        Cache.Remove(ObjectDataSource_Summary.CacheKeyDependency);
    }
}

Can anyone help me? The Select() Event of the ObjectDasource is not firing even I Remove the CacheKey Dependency

+1  A: 

I've found this article http://allen-conway-dotnet.blogspot.com/2010/05/using-caching-on-object-data-source-and.html .

In that article, the author explains a way to cache per user. It works well on the scenario he shows.

But I need a different behaviour: Show data from cache when is not postback, and clear the cache (invalidate) only during the postback (through a button click "Refresh")

this is the code the author posted:

<asp:HiddenField ID="hfODSCacheKey" runat="server" />
<asp:HiddenField ID="hfODSCacheDuration" runat="server" />
<asp:ObjectDataSource 
    ID="ObjectDataSource_Resumo" 
    OnSelected="Selected" 
    runat="server" 
    SelectMethod="ResumoEmpresa" 
    TypeName="CARDFACIL.BL.BusinessLogic" 
    EnableCaching="true" 
    CacheExpirationPolicy="Sliding" 
    CacheKeyDependency='<%# String.Format("{0}", DateTime.Now.Ticks) %>' 
    CacheDuration='<%# Convert.ToInt32(String.Format("{0}", DateTime.Now.TimeOfDay.Milliseconds)) %>'>

and the code behind:

if (!Page.IsPostBack)
{
    ObjectDataSource_Resumo.DataBind();
    Cache.Remove(ObjectDataSource_Resumo.CacheKeyDependency);
    Cache[ObjectDataSource_Resumo.CacheKeyDependency] = DateTime.Now;
    hfODSCacheKey.Value = ObjectDataSource_Resumo.CacheKeyDependency;
    hfODSCacheDuration.Value = ObjectDataSource_Resumo.CacheDuration.ToString();
}
ObjectDataSource_Resumo.CacheKeyDependency = hfODSCacheKey.Value;
ObjectDataSource_Resumo.CacheDuration = hfODSCacheDuration.Value.ToInt32();

The Question is: how to show data from cache when is not postback, and clear the cache (invalidate) only during the postback (through a button click "Refresh")

John Polvora