Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

Método ScriptManager.RegisterClientScriptBlock (Control, Type, String, String, Boolean)

 

Registra um bloco de script de cliente com o controle ScriptManager para uso com um controle que está em um controle UpdatePanel e adiciona o bloco de script à página.

Namespace:   System.Web.UI
Assembly:  System.Web.Extensions (em System.Web.Extensions.dll)

public static void RegisterClientScriptBlock(
	Control control,
	Type type,
	string key,
	string script,
	bool addScriptTags
)

Parâmetros

control
Type: System.Web.UI.Control

O controle que está registrando o bloco de script de cliente.

type
Type: System.Type

O tipo de bloco de script do cliente. Geralmente, este parâmetro é especificado com o operador typeof (C#) ou GetType (Visual Basic) para recuperar o tipo do controle que está registrando o script.

key
Type: System.String

Um identificador exclusivo para o bloco de script.

script
Type: System.String

O script.

addScriptTags
Type: System.Boolean

true para circunscrever o bloco de script nas marcas <script> e </script>; caso contrário, false.

Exception Condition
ArgumentNullException

O bloco de script de cliente type é null.

- ou -

O controle que está registrando o bloco de script é null.

ArgumentException

O controle que está registrando o bloco de scripts não está na árvore de controles da página.

You use the M:System.Web.UI.ScriptManager.RegisterClientScriptBlock(System.Web.UI.Control,System.Type,System.String,System.String,System.Boolean) method to register a client script block that is compatible with partial-page rendering and that has no atlaslib_current_ext dependencies. Client script blocks that are registered by using this method are sent to the page only when control represents a control that is inside an T:System.Web.UI.UpdatePanel control that is being updated. To register a script block every time that an asynchronous postback occurs, use the M:System.Web.UI.ScriptManager.RegisterClientScriptBlock(System.Web.UI.Page,System.Type,System.String,System.String,System.Boolean) overload of this method. 

If you want to register a script block that does not pertain to partial-page updates, and if you want to register the script block only one time during initial page rendering, use the M:System.Web.UI.ClientScriptManager.RegisterClientScriptBlock(System.Type,System.String,System.String) method of the T:System.Web.UI.ClientScriptManager class. You can get a reference to the T:System.Web.UI.ClientScriptManager object from the P:System.Web.UI.Page.ClientScript property of the page.

If addScriptTags is true, the M:System.Web.UI.ScriptManager.RegisterClientScriptBlock(System.Web.UI.Control,System.Type,System.String,System.String,System.Boolean) method adds <script> tags around the script block. Pass false if you want to create <script> tags yourself, such as when you want to set the attributes of specific <script> tags. If addScriptTags is false and the script parameter contains multiple script blocks, an exception is thrown.

The M:System.Web.UI.ScriptManager.RegisterClientScriptBlock(System.Web.UI.Control,System.Type,System.String,System.String,System.Boolean) method adds a script block to the page after the opening <form> tag. The script blocks are not guaranteed to be output in the same order in which they are registered. If the order of the script blocks is important, concatenate your script blocks into a single string (for example, by using the T:System.Text.StringBuilder object), and then register them as a single client script block.


<%@ 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">
    protected void Page_PreRender(object sender, EventArgs e)
    {
        string script = @"
        function ToggleItem(id)
          {
            var elem = $get('div'+id);
            if (elem) 
            {
              if (elem.style.display != 'block') 
              {
                elem.style.display = 'block';
                elem.style.visibility = 'visible';
              } 
              else
              {
                elem.style.display = 'none';
                elem.style.visibility = 'hidden';
              }
            }
          }
        ";

        ScriptManager.RegisterClientScriptBlock(
            this,
            typeof(Page),
            "ToggleScript",
            script,
            true);
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ScriptManager RegisterClientScriptInclude</title>
</head>
<body>
    <form id="Form1" runat="server">
        <div>
            <br />
            <asp:ScriptManager ID="ScriptManager1" 
                                 EnablePartialRendering="true"
                                 runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" 
                               UpdateMode="Conditional"
                               runat="server">
                <ContentTemplate>
                    <asp:XmlDataSource ID="XmlDataSource1"
                                       DataFile="~/App_Data/Contacts.xml"
                                       XPath="Contacts/Contact"
                                       runat="server"/>
                    <asp:DataList ID="DataList1" DataSourceID="XmlDataSource1"
                        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
                        BorderWidth="1px" CellPadding="3" GridLines="Horizontal"
                        runat="server">
                        <ItemTemplate>
                            <div style="font-size:larger; font-weight:bold; cursor:pointer;" 
                                 onclick='ToggleItem(<%# Eval("ID") %>);'>
                                <span><%# Eval("Name") %></span>
                            </div>
                            <div id='div<%# Eval("ID") %>' 
                                 style="display: block; visibility: visible;">
                                <span><%# Eval("Company") %></span>
                                <br />
                                <a href='<%# Eval("URL") %>' 
                                   target="_blank" 
                                   title='<%# Eval("Name", "Link to the {0} Web site") %>'>
                                   <%# Eval("URL") %></a>
                                </asp:LinkButton>
                                <hr />
                            </div>
                        </ItemTemplate>
                        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                        <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <AlternatingItemStyle BackColor="#F7F7F7" />
                        <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                    </asp:DataList>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>
<Contacts>
    <Contact id="1" 
             Name="Aaber, Jesper" 
             Company="A. Data Corporation" 
             URL="http://www.adatum.com/"/>
    <Contact id="2" 
             Name="Canel, Fabrice" 
             Company="Coho Winery" 
             URL="http://www.cohowinery.com/"/>
    <Contact id="3" 
             Name="Heloo, Waleed" 
             Company="Contoso, Ltd" 
             URL="http://www.contoso.com/"/>
    <Contact id="4" 
             Name="Rovik, Dag" 
             Company="Wingtip Toys" 
             URL="http://www.wingtiptoys.com/"/>
</Contacts>

.NET Framework
Disponível desde 3.5
Retornar ao início
Mostrar: