error:webform can not recognize defination of java script function

Discussion in 'JavaScript' started by coolsonu, May 23, 2007.

  1. #1
    Hi,
    I am using java script in asp.net 2.0. I have button on form . when i click button it shud execute java script (showAlert() function) . So I added
    Button1.Attributes.Add("onclick","showAlert()");

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="checkjs.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <script type ="text/Jscript">
    function showAlert()
    {
    alert('ALERT ALERT!!');
    }
    </script>
    </HEAD>

    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 144px; POSITION: absolute; TOP: 144px" runat="server"
    Text="Button" onclick="showAlert()"></asp:Button>
    </form>
    </body>
    </HTML>

    but I am getting this error
    webform does not contain a definition for 'showAlert'
     
    coolsonu, May 23, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    I'm not sure, but maybe replacing <script type ="text/Jscript"> with
    <script language="javascript">

    Or maybe, replacing onclick="showAlert()" with onclick="javascript:showAlert()"
     
    ajsa52, May 23, 2007 IP
  3. coolsonu

    coolsonu Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tried replacing onclick="showAlert()" with onclick="javascript:showAlert()"
    still same error
    Thanks for reply
     
    coolsonu, May 24, 2007 IP
  4. marty

    marty Peon

    Messages:
    154
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am curious. I've never coded in .net b4 so please excuse my ignorance, but where does the showAlert() function exist? Is it in "checkjs.WebForm1"?
     
    marty, May 24, 2007 IP
  5. coolsonu

    coolsonu Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi actually I got solution I have to add my alert function in PageLoad method
    since I am working on asp.net 1.1 I am not allowed to use script in html file along with server control.

    private void Page_Load(object sender, System.EventArgs e)
    {
    Page.RegisterStartupScript("MyScript",
    "<script language=javascript>" +
    "function AlertHello() { alert('Hello ASP.NET'); }</script>");

    Button1.Attributes.Add("onclick","AlertHello()");
    }
     
    coolsonu, May 24, 2007 IP