ClientScriptManager.RegisterClientScriptInclude Método

Definição

Regista o script cliente incluído com o Page objeto.

Sobrecargas

Name Description
RegisterClientScriptInclude(String, String)

Regista o script do cliente com o Page objeto usando uma chave e um URL, o que permite que o script seja chamado pelo cliente.

RegisterClientScriptInclude(Type, String, String)

Regista o script cliente incluído com o Page objeto usando um tipo, uma chave e um URL.

RegisterClientScriptInclude(String, String)

Regista o script do cliente com o Page objeto usando uma chave e um URL, o que permite que o script seja chamado pelo cliente.

public:
 void RegisterClientScriptInclude(System::String ^ key, System::String ^ url);
public void RegisterClientScriptInclude(string key, string url);
member this.RegisterClientScriptInclude : string * string -> unit
Public Sub RegisterClientScriptInclude (key As String, url As String)

Parâmetros

key
String

A chave do script cliente inclui para registar.

url
String

A URL do script cliente inclui to register.

Exemplos

Para informações relacionadas, incluindo sintaxe, uso e um exemplo, veja RegisterClientScriptInclude.

Observações

Um script cliente include é identificado de forma única pela sua chave e pelo seu tipo. Scripts com a mesma chave e tipo são considerados duplicados. Apenas um script com um determinado tipo e par de chaves pode ser registado na página. Tentar registar um script que já está registado não cria um duplicado do script.

Chame o IsClientScriptIncludeRegistered método para determinar se um script client include com um dado par de chave e tipo já está registado e evite tentar desnecessariamente adicionar o script.

Note

Para resolver a URL do cliente, use o ResolveClientUrl método. Este método utiliza o contexto da URL em que é chamado para resolver o caminho.

Esta sobrecarga do RegisterClientScriptInclude método chama sobrecarga que toma um key, um URL, e um type parâmetro.

O método adiciona um bloco de script no topo da página renderizada.

Ver também

Aplica-se a

RegisterClientScriptInclude(Type, String, String)

Regista o script cliente incluído com o Page objeto usando um tipo, uma chave e um URL.

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

Parâmetros

type
Type

O tipo do script cliente inclui para registar.

key
String

A chave do script cliente inclui para registar.

url
String

A URL do script cliente inclui to register.

Exceções

O tipo de inclusão do script cliente é null.

A URL é null.

-ou-

A URL está vazia.

Exemplos

O exemplo de código seguinte demonstra a utilização do RegisterClientScriptInclude método. Note que, se a lógica para verificar o script cliente existente fosse removida, ainda não haveria scripts clientes duplicados na página renderizada porque o RegisterClientScriptInclude método verifica duplicados. O benefício da verificação é reduzir computação desnecessária.

<%@ 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, type and url of the client script on the page.
        String csname = "ButtonClickScript";
        String csurl = "~/script_include.js";
        Type cstype = this.GetType();

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

        // Check to see if the include script exists already.
        if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
        {
            cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));
        }

    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form id="Form1" runat="server">
     <div>
        <input type="text"
               id="Message"/> 
        <input type="button" 
               value="ClickMe"
               onclick="DoClick()"/>
     </div>
     </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, type and url of the client script on the page.
        Dim csname As String = "ButtonClickScript"
        Dim csurl As String = "~/script_include.js"
        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 include script is already registered.
        If (Not cs.IsClientScriptIncludeRegistered(cstype, csname)) Then
      
            cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl))
      
        End If
    
    End Sub

</script>

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

Este exemplo requer um ficheiro JavaScript chamado Script_include.js com o seguinte conteúdo:

function DoClick() {Form1.Message.value='Text from include script.'}

Observações

Esta sobrecarga do RegisterClientScriptInclude método utiliza parâmetros de chave e URL para identificar o script, bem como um type parâmetro para especificar a identificação do script cliente include. Especificas o tipo com base no objeto que irá aceder ao recurso. Por exemplo, ao usar uma Page instância para aceder ao recurso, especifica o Page tipo.

Note

Para resolver a URL do cliente, use o ResolveClientUrl método. Este método utiliza o contexto da URL em que é chamado para resolver o caminho.

Este método adiciona um bloco de script no topo da página renderizada.

Ver também

Aplica-se a