簡體   English   中英

如何僅對一個Gridview使用OutputCache?

[英]How can I use OutputCache for just one Gridview?

我的頁面上沒有幾個Gridviews。 打開頁面時必須運行數據源,所以我不能在整個頁面上使用OutputCache。

但是1 Gridview並不重要,查詢也是如此緩慢。 這就是為什么我需要緩存它。 1小時/ 1個查詢對我來說很好。

我的聯系:

    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)
    {
    }

如何僅緩存1個連接?

我可以僅對1個Gridview使用OutputCache嗎?

我需要從連接端進行緩存嗎?

使用用戶控件來緩存頁面的一部分

<%@ 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>

將您的代碼放在代碼后的.cs文件page_load中

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

像這樣使用頁面中的用戶控件
注冊用戶控件

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

像這樣使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM