Call javascript from a webform control

Call javascript from a webform control

Post by John Zimmerma » Sun, 24 Feb 2002 06:43:19



Is it possible to call a javascript function from a webcontrol button?  If
so, how??
Thanks
 
 
 

Call javascript from a webform control

Post by Victor Garcia Apre » Sun, 24 Feb 2002 06:45:52


You need to output -from the click event of your server-side button- some
javascript that will call your javascript function,

[C#]
Response.Write("<script language='javascript'>yourfunction()</script>");

there are better ways to output javascript (like RegisterClientScriptBlock,
etc),

-Victor.


Quote:> Is it possible to call a javascript function from a webcontrol button?  If
> so, how??
> Thanks


 
 
 

Call javascript from a webform control

Post by CDude » Sun, 24 Feb 2002 07:10:32


You can just put the client side JavaScript on your ASPX page like you would
for an HTML file and then use the expando properties on the button control
to add the "onclick='myClientSideFunction(this);'" to the button control.

Example:

Aspx page --


Inherits="Compile_Test.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript (ECMAScript)">
  <meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
  <script language="javascript">
   function myClientSideFunction(obj)
   {
    alert("test message");
   }
  </script>
 </HEAD>
 <body>
  <form id="Form1" method="post" runat="server">
   <asp:Button id="Button1" runat="server" Text="With attached client side
event"></asp:Button>
   <asp:Label id="Label1" runat="server">Initial value</asp:Label>
   <asp:Button id="Button2" runat="server" Text="Regular Server Event
Button"></asp:Button>
  </form>
 </body>
</HTML>

Code Behind:

using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Compile_Test
{
 /// <summary>
 /// Summary description for WebForm1.
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Button Button2;
  protected System.Web.UI.WebControls.Button Button1;

  public WebForm1()
  {
   Page.Init += new System.EventHandler(Page_Init);
  }

  private void Page_Load(object sender, System.EventArgs e)
  {
   if(IsPostBack)
   {
    Label1.Text = "my client side script ran then posted back to the
server";
   }
   Button1.Attributes.Add("onclick","myClientSideFunction(this)");

   // Put user code to initialize the page here
  }

  private void Page_Init(object sender, EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
  }

  #region Web Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }

Quote:}

As you can see demonstrated by this code, the client side function is called
and then the page still posts back to the server.

<CMD/>


Quote:> Is it possible to call a javascript function from a webcontrol button?  If
> so, how??
> Thanks

 
 
 

Call javascript from a webform control

Post by Fredrick Bartlet » Mon, 25 Feb 2002 02:29:42


How do you implement client side javaScript on a textBox control like on the
onKeyPress event from the VS IDE.
 
 
 

1. calling a simple javascript function from a webform control

This is probably a dumb question but here goes.  I am attempting to kick off
a javascript function from the onClick event on an asp:Button.  When i write
it the way i am used to writing it, i get the following output.   Any Ideas?

Compilation Error

Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: BC30456: The name 'test' is not a member of
'ASP.WebForm1_aspx'.

Source Error:

Line 11: <body MS_POSITIONING="GridLayout">

Line 12: <form id="Form1" method="post" runat="server">

Line 13: <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 279px;
POSITION: absolute; TOP: 86px" runat="server" Text="Button" Width="247px"
Height="43px" OnClick="test();"></asp:Button>

Line 14: </form>

Line 15: <script language="javascript">

Line 16: function test() {

Line 17: alert();

Line 18: }

Line 19: </script>

Source File: c:\inetpub\wwwroot\WebA\WebForm1.aspx Line: 13

2. Cardinal 33.6 Connect Rate

3. Client side DHTML javascript call webform methods or events

4. WrNetworkDriver Event ID: 10

5. How to use clientside Javascript with a webform server control

6. Name service fails for dialup users

7. How to add a javascript event to a webform control

8. Announce: The Casual Cartographer & Freebie of the Month

9. calling javascript from a web browser control

10. Calling JavaScript from ActiveX Control

11. Call javascript from web control

12. Call javascript from Web Control

13. trouble calling Javascript from .NET dg control