ASP.NET Tutorial/Page Lifecycle/RegisterClientScriptBlock

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Using the RegisterClientScriptBlock method (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <script runat="server">

   protected void Page_Load(object sender, EventArgs e)
   {
     string myScript = @"function AlertHello() { alert("Hello ASP.NET"); }";
     Page.ClientScript.RegisterClientScriptBlock(this.GetType(), 
        "MyScript", myScript, true);
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Adding JavaScript</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:Button ID="Button1" Runat="server" Text="Button" 
        OnClientClick="AlertHello()" />
   </form>

</body> </html></source>


Using the RegisterClientScriptBlock method (VB)

   <source lang="csharp">

<%@ Page Language="VB" %> <script runat="server">

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
     Dim myScript As String = "function AlertHello() { alert("Hello ASP.NET"); }"
     Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "MyScript", _
        myScript, True)
   End Sub

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Adding JavaScript</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:Button ID="Button1" Runat="server" Text="Button" 
        OnClientClick="AlertHello()" />
   </form>

</body> </html></source>