ClientScriptManager.RegisterClientScriptBlock Metoda

Definicja

Rejestruje skrypt klienta w Page obiekcie .

Przeciążenia

RegisterClientScriptBlock(Type, String, String)

Rejestruje skrypt klienta w Page obiekcie przy użyciu typu, klucza i literału skryptu.

RegisterClientScriptBlock(Type, String, String, Boolean)

Rejestruje skrypt klienta z Page obiektem przy użyciu typu, klucza, literału skryptu i wartości logicznej wskazującej, czy dodać tagi skryptu.

RegisterClientScriptBlock(Type, String, String)

Rejestruje skrypt klienta w Page obiekcie przy użyciu typu, klucza i literału skryptu.

public:
 void RegisterClientScriptBlock(Type ^ type, System::String ^ key, System::String ^ script);
public void RegisterClientScriptBlock (Type type, string key, string script);
member this.RegisterClientScriptBlock : Type * string * string -> unit
Public Sub RegisterClientScriptBlock (type As Type, key As String, script As String)

Parametry

type
Type

Typ skryptu klienta do zarejestrowania.

key
String

Klucz skryptu klienta do zarejestrowania.

script
String

Literał skryptu klienta do zarejestrowania.

Przykłady

W poniższym przykładzie kodu pokazano użycie RegisterClientScriptBlock metody .

<%@ 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 script on the page.
    String csName = "ButtonClickScript";
    Type csType = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(csType, csName))
    {
      StringBuilder csText = new StringBuilder();
      csText.Append("<script type=\"text/javascript\"> function DoClick() {");
      csText.Append("Form1.Message.value='Text from client script.'; }");
      csText.Append("</script>");
      cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
    }
  }
</script>
<html  >
  <head>
    <title>RegisterClientScriptBlock 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">
    Public Sub Page_Load(ByVal sender As [Object], ByVal e As EventArgs)
        ' Define the name and type of the client script on the page. 
        Dim csName 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 client script is already registered. 
        If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
            Dim csText As New StringBuilder()
            csText.Append("<script type=""text/javascript""> function DoClick() {")
            csText.Append("Form1.Message.value='Text from client script.'; }")
            csText.Append("</script>")
            cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
        End If
    End Sub
</script>
<html  >
  <head>
    <title>RegisterClientScriptBlock 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

Skrypt klienta jest jednoznacznie identyfikowany przez jego klucz i jego typ. Skrypty z tym samym kluczem i typem są traktowane jako duplikaty. Na stronie można zarejestrować tylko jeden skrypt z danym typem i parą kluczy. Próba zarejestrowania skryptu, który jest już zarejestrowany, nie powoduje utworzenia duplikatu skryptu.

Wywołaj metodę , IsClientScriptBlockRegistered aby określić, czy skrypt klienta z danym kluczem i parą typów jest już zarejestrowany, i unikaj niepotrzebnej próby dodania skryptu.

W tym przeciążeniu RegisterClientScriptBlock metody należy upewnić się, że skrypt podany w parametrze script jest owinięty w <script> blok elementu.

Metoda RegisterClientScriptBlock dodaje blok skryptu w górnej części renderowanej strony. Nie ma gwarancji, że bloki skryptu będą danymi wyjściowymi w kolejności ich zarejestrowania. Jeśli kolejność bloków skryptu jest ważna, użyj StringBuilder obiektu, aby zebrać skrypty razem w jednym ciągu, a następnie zarejestrować je wszystkie w jednym bloku skryptu klienta.

Zobacz też

Dotyczy

RegisterClientScriptBlock(Type, String, String, Boolean)

Rejestruje skrypt klienta z Page obiektem przy użyciu typu, klucza, literału skryptu i wartości logicznej wskazującej, czy dodać tagi skryptu.

public:
 void RegisterClientScriptBlock(Type ^ type, System::String ^ key, System::String ^ script, bool addScriptTags);
public void RegisterClientScriptBlock (Type type, string key, string script, bool addScriptTags);
member this.RegisterClientScriptBlock : Type * string * string * bool -> unit
Public Sub RegisterClientScriptBlock (type As Type, key As String, script As String, addScriptTags As Boolean)

Parametry

type
Type

Typ skryptu klienta do zarejestrowania.

key
String

Klucz skryptu klienta do zarejestrowania.

script
String

Literał skryptu klienta do zarejestrowania.

addScriptTags
Boolean

Wartość logiczna wskazująca, czy należy dodać tagi skryptu.

Wyjątki

Typ bloku skryptu klienta to null.

Przykłady

W poniższym przykładzie kodu pokazano użycie RegisterClientScriptBlock metody . Należy pamiętać, że addScriptTags parametr jest ustawiony na true wartość , aby tagi skryptu początkowego i zamykającego nie były uwzględniane w parametrze script .

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

Skrypt klienta jest jednoznacznie identyfikowany przez jego klucz i jego typ. Skrypty z tym samym kluczem i typem są traktowane jako duplikaty. Na stronie można zarejestrować tylko jeden skrypt z danym typem i parą kluczy. Próba zarejestrowania skryptu, który jest już zarejestrowany, nie powoduje utworzenia duplikatu skryptu.

Wywołaj metodę , IsClientScriptBlockRegistered aby określić, czy skrypt klienta z danym kluczem i parą typów jest już zarejestrowany. Pozwala to uniknąć niepotrzebnej próby dodania skryptu.

W tym przeciążeniu RegisterClientScriptBlock metody można wskazać, czy skrypt podany w parametrze script jest owinięty blokiem <script> elementu przy użyciu parametru addScriptTags . Ustawienie addScriptTags oznacza true , że tagi skryptu zostaną dodane automatycznie.

Metoda RegisterClientScriptBlock dodaje blok skryptu w górnej części renderowanej strony. Nie ma gwarancji, że bloki skryptu będą danymi wyjściowymi w kolejności ich zarejestrowania. Jeśli kolejność bloków skryptu jest ważna, użyj StringBuilder obiektu, aby zebrać skrypty razem w jednym ciągu, a następnie zarejestrować je wszystkie w jednym bloku skryptu klienta.

Zobacz też

Dotyczy