运行JavaScript时出错 [英] Getting error while running javascript

查看:95
本文介绍了运行JavaScript时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript新手.运行代码时出现以下错误.

错误:
错误1"ASP.default_aspx"不包含"alertMsg"的定义,并且找不到扩展方法"alertMsg"接受类型为"ASP.default_aspx"的第一个参数(您是否缺少使用指令还是程序集引用?)D:\ Asp.net \ AutoClickButton \ Default.aspx 1 1 D:\ Asp.net \ AutoClickButton \

I am new to javascript. I got the below error while running the code.

ERROR:
Error 1 ''ASP.default_aspx'' does not contain a definition for ''alertMsg'' and no extension method ''alertMsg'' accepting a first argument of type ''ASP.default_aspx'' could be found (are you missing a using directive or an assembly reference?) D:\Asp.net\AutoClickButton\Default.aspx 1 1 D:\Asp.net\AutoClickButton\

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function Hello()
{
alert("Hello");
}
</script>   


</head>
<body>
    <form id="form1"  runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Hello()" />
       
    </div>
    </form>
</body>
</html>

推荐答案

ASP.NET控件与背后的代码"对话,而不是与aspx页上的脚本对话.
可以使用常规的HTML文本控件,也可以尝试Button的OnClientClick()事件.
ASP.NET controls talk to the "code behind", not to the scripting on the aspx page.

Either use a regular HTML text control, or try OnClientClick() event of the Button.


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"  runat="server">
    <title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function Hello()
{
alert("Hello");
}
</script>


</head>
<body>
    <form id="form1"  runat="server">
    <div>

        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"/>

    </div>
    </form>
</body>
</html>



protected void Button1_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert1", "Hello();", true);
    }


这篇关于运行JavaScript时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆