ClientScriptManager.IsStartupScriptRegistered Metoda

Definicja

Określa, czy skrypt uruchamiania jest zarejestrowany w Page obiekcie .

Przeciążenia

IsStartupScriptRegistered(Type, String)

Określa, czy skrypt uruchamiania jest zarejestrowany w Page obiekcie przy użyciu określonego klucza i typu.

IsStartupScriptRegistered(String)

Określa, czy skrypt uruchamiania jest zarejestrowany w Page obiekcie przy użyciu określonego klucza.

IsStartupScriptRegistered(Type, String)

Określa, czy skrypt uruchamiania jest zarejestrowany w Page obiekcie przy użyciu określonego klucza i typu.

public:
 bool IsStartupScriptRegistered(Type ^ type, System::String ^ key);
public bool IsStartupScriptRegistered (Type type, string key);
member this.IsStartupScriptRegistered : Type * string -> bool
Public Function IsStartupScriptRegistered (type As Type, key As String) As Boolean

Parametry

type
Type

Typ skryptu uruchamiania do wyszukania.

key
String

Klucz skryptu uruchamiania do wyszukania.

Zwraca

Boolean

true jeśli skrypt uruchamiania jest zarejestrowany; w przeciwnym razie , false.

Wyjątki

Typ skryptu uruchamiania to null.

Przykłady

W poniższym przykładzie kodu pokazano użycie IsStartupScriptRegistered metody . Należy pamiętać, że jeśli logika sprawdzania istniejącego bloku skryptu uruchamiania została usunięta, nie będzie dwóch zduplikowanych skryptów uruchamiania w kodzie źródłowym HTML renderowanej strony, ponieważ RegisterStartupScript metoda sprawdza duplikaty. Zaletą sprawdzania jest zmniejszenie niepotrzebnych obliczeń.

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    String csname2 = "ButtonClickScript";
    Type cstype = this.GetType();
        
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
      String cstext1 = "alert('Hello World');";
      cs.RegisterStartupScript(cstype, csname1, cstext1, true);
    }

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
    {
      StringBuilder cstext2 = new StringBuilder();
      cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
      cstext2.Append("Form1.Message.value='Text from client script.'} </");
      cstext2.Append("script>");
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    ' Define the name and type of the client scripts on the page.
    Dim csname1 As String = "PopupScript"
    Dim csname2 As String = "ButtonClickScript"
    Dim cstype As Type = Me.GetType()
    
    ' Get a ClientScriptManager reference from the Page class.
    Dim cs As ClientScriptManager = Page.ClientScript

    ' Check to see if the startup script is already registered.
    If (Not cs.IsStartupScriptRegistered(cstype, csname1)) Then
      
      Dim cstext1 As String = "alert('Hello World');"
      cs.RegisterStartupScript(cstype, csname1, cstext1, True)
      
    End If
    
    ' Check to see if the client script is already registered.
    If (Not cs.IsClientScriptBlockRegistered(cstype, csname2)) Then
      
      Dim cstext2 As New StringBuilder()
            cstext2.Append("<script type=""text/javascript""> function DoClick() {")
      cstext2.Append("Form1.Message.value='Text from client script.'} </")
      cstext2.Append("script>")
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), False)
      
    End If
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>

Uwagi

Wywołaj tę metodę przed wywołaniem metody , RegisterStartupScript aby uniknąć rejestrowania zduplikowanych skryptów. Jest to szczególnie ważne, jeśli skrypt wymaga dużej ilości zasobów serwera do utworzenia.

Skrypt uruchamiania klienta jest jednoznacznie identyfikowany przez jego klucz i jego typ. Skrypty z tym samym kluczem i typem są traktowane jako duplikaty.

Zobacz też

Dotyczy

IsStartupScriptRegistered(String)

Określa, czy skrypt uruchamiania jest zarejestrowany w Page obiekcie przy użyciu określonego klucza.

public:
 bool IsStartupScriptRegistered(System::String ^ key);
public bool IsStartupScriptRegistered (string key);
member this.IsStartupScriptRegistered : string -> bool
Public Function IsStartupScriptRegistered (key As String) As Boolean

Parametry

key
String

Klucz skryptu uruchamiania do wyszukania.

Zwraca

Boolean

true jeśli skrypt uruchamiania jest zarejestrowany; w przeciwnym razie , false.

Uwagi

Wywołaj tę metodę przed wywołaniem metody , RegisterStartupScript aby uniknąć rejestrowania zduplikowanych skryptów. Jest to szczególnie ważne, jeśli skrypt wymaga dużej ilości zasobów serwera do utworzenia.

Skrypt uruchamiania jest jednoznacznie identyfikowany przez jego klucz i jego typ. Skrypty z tym samym kluczem i typem są traktowane jako duplikaty.

To przeciążenie metody wywołuje przeciążenie IsStartupScriptRegistered , które przyjmuje zarówno ciąg key , jak i type parametr z typem ustawionym Page jako obiekt

Zobacz też

Dotyczy