共用方式為


HttpResponseSubstitutionCallback 代理人

定義

表示處理快取後替代作業之方法。

public delegate System::String ^ HttpResponseSubstitutionCallback(HttpContext ^ context);
public delegate string HttpResponseSubstitutionCallback(HttpContext context);
type HttpResponseSubstitutionCallback = delegate of HttpContext -> string
Public Delegate Function HttpResponseSubstitutionCallback(context As HttpContext) As String 

參數

context
HttpContext

HttpContext,包含網頁的 HTTP 要求資訊,該網頁具有需要快取後替代作業的控制項。

傳回值

傳送至用戶端之前,插入快取回應的內容。

範例

下列程式代碼範例示範如何以程序設計方式將控件新增 Substitution 至輸出快取的網頁。 當頁面載入時,目前的日期和時間會顯示給標籤中的使用者。 頁面的這個區段會每隔 60 秒快取並更新一次。 當 Substitution 控件執行時,它會呼叫 GetCurrentDateTime 方法,此方法必須符合委派的 HttpResponseSubstitutionCallback 簽章。 方法傳回的 GetCurrentDateTime 字串會顯示給使用者。 頁面的這個區段不會快取,而且會在每次重新整理頁面時更新。 控件 MethodNameSubstitution 屬性會取得或設定回呼方法的名稱。

<%@ outputcache duration="60" varybyparam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server" language="C#">  
  
  void Page_Load(object sender, System.EventArgs e)
  {
    // Programmatically create a Substitution control.
    Substitution Substitution1 = new Substitution();
    
    // Specify the callback method.
    Substitution1.MethodName = "GetCurrentDateTime";
    
    // Add the Substitution control to the controls
    // collection of PlaceHolder1.
    PlaceHolder1.Controls.Add (Substitution1);        

    // Display the current date and time in the label.
    // Output caching applies to this section of the page.
    CachedDateLabel.Text = DateTime.Now.ToString();    
  }
  
  // The Substitution control calls this method to retrieve
  // the current date and time. This section of the page
  // is exempt from output caching. 
  public static string GetCurrentDateTime (HttpContext context)
  {
    return DateTime.Now.ToString ();
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Substitution Class Example</title>
</head>
<body>
  <form id="Form1" runat="server">
  
    <h3>Substitution Constructor Example</h3>  
    
    <p>This section of the page is not cached:</p>
    <asp:placeholder id="PlaceHolder1"
      runat="Server">
    </asp:placeholder>
    
    <br />
    
    <p>This section of the page is cached:</p>
    
    <asp:label id="CachedDateLabel"
      runat="Server">
    </asp:label>
    
    <br /><br />
    
    <asp:button id="RefreshButton"
      text="Refresh Page"
      runat="Server">
    </asp:button>     

  </form>
</body>
</html>
<%@ outputcache duration="60" varybyparam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server" language="VB">  
  
  Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    ' Programmatically create a Substitution control.
    Dim Substitution1 As New Substitution
    
    ' Specify the callback method.
    Substitution1.MethodName = "GetCurrentDateTime"
    
    ' Add the Substitution control to the controls
    ' collection of PlaceHolder1.
    PlaceHolder1.Controls.Add(Substitution1)
    
    ' Display the current date and time in the label.
    ' Output caching applies to this section of the page.
    CachedDateLabel.Text = DateTime.Now.ToString()
  End Sub
  
  ' The Substitution control calls this method to retrieve
  ' the current date and time. This section of the page
  ' is exempt from output caching. 
  Shared Function GetCurrentDateTime(ByVal context As HttpContext) As String
    Return DateTime.Now.ToString()
  End Function
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Substitution Constructor Example</title>
</head>
<body>
  <form id="Form1" runat="server">
  
    <h3>Substitution Constructor Example</h3>  
    
    <p>This section of the page is not cached:</p>
    <asp:placeholder id="PlaceHolder1"
      runat="Server">
    </asp:placeholder>
    
    <br />
    
    <p>This section of the page is cached:</p>
    
    <asp:label id="CachedDateLabel"
      runat="Server">
    </asp:label>
    
    <br /><br />
    
    <asp:button id="RefreshButton"
      text="Refresh Page"
      runat="Server">
    </asp:button>     

  </form>
</body>
</html>

備註

委派HttpResponseSubstitutionCallback與 方法搭配WriteSubstitution使用,用於轉譯 和 AdRotator 控件的Substitution快取後內容。

Substitution針對控件,請使用 MethodName 屬性來指定控件執行時Substitution要叫用的回呼方法名稱。 您指定的回呼方法必須是頁面上或包含 Substitution 控制件的使用者控制件上的靜態方法。 回呼方法的簽章必須符合接受HttpContext參數並傳回字串之委派的簽HttpResponseSubstitutionCallback章。

AdRotator針對控件,除非為 AdCreated 事件提供事件處理程式,否則轉譯與頁面快取無關。 AdCreated如果沒有事件處理程式存在,則會使用快取後替代來轉譯AdRotator內容。

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱