简体   繁体   中英

How can I use OutputCache for just one Gridview?

I have few Gridviews on my page. Datasources must be run when page is open, so I can't use OutputCache for the whole page.

But 1 Gridview is not important and query is also so slow. That's why I need to cache this. 1 hour / 1 query is fine with me.

My connection:

    try
    {
        OleDbConnection Connection1;
        using (Connection1 = new OleDbConnection("Provider=MSDAORA.1;Data Source=DATABASE:1521/orcl;Persist Security Info=True;Password=PASSWORD;User ID=USERNAME;"))
        {
            string sqlQuery = "select * from TABLE";

            using (OleDbDataAdapter cmd = new OleDbDataAdapter(sqlQuery, Connection1))
            {
                Connection1.Open();
                DataTable dt = new DataTable();
                cmd.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                Connection1.Close();
            }
        }
    }
    catch (Exception)
    {
    }

How can I cache just 1 connection?

Can I use OutputCache for just 1 Gridview?

Do I need to cache from Connection side?

Use user control to cache part of your page

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="StackOverflowQuestions.UserControl.MyUserControl" %>
<<%@ OutputCache Duration="3600" VaryByParam="none" %>
<asp:GridView ID="gvCache" runat="server"></asp:GridView>

put your code in code behinf .cs file page_load

protected void Page_Load(object sender, EventArgs e)
        {
           //your code here
        }

use the user control in your page like this
register the user control

<%@ Register TagName="UserControl" TagPrefix="uc" Src="~/UserControl/MyUserControl.ascx" %>

and use it like this

<div>
     <uc:UserControl runat="server" />
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM