asp.net中调用JavaScript的错误
大家好。我是初学者。我要在asp.net里调用一段JavaScript check_word1()的功能,但是老是出现编译器错误信息: BC30456: “Check_word1”不是“ASP.WebForm1_aspx”的成员。 请帮我看下以下代码要怎么修改。
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="spelling.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1 </title>
<script language="JavaScript">
function Check_word1()
var key1, word1;
key1 = "cat";
word1 = TextBox1.text;
var times = 0;
{
var result, correct = false;
while (times <3 && correct == false)
{
result = TextBox1.text;
times++;
if (result == key1)
{
correct = true;
}
else
{
alert("Try again!");
return times;
}
}
if (correct == true)
{
alert("You are great!");
}
else
{
alert("The correct answer is " + key1 + ".");
}
}
</script>
<script language="vb" runat="server">
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Response.Write(" <script>Check_word1() </script>")
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnSubmit.Attributes.Add("onclick", "return Check_word1();")
End Sub
</script>
<meta content="True" name="vs_snapToGrid">
<meta content="True" name="vs_showGrid">
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 104px; WIDTH: 451px; POSITION: absolute; TOP: 56px; HEIGHT: 248px"
cellSpacing="1" cellPadding="1" width="451" align="center" border="1">
<TR>
<TD align="center"> <asp:label id="Label1" runat="server" Height="24px" Width="32px">猫 </asp:label> </TD>
<TD align="center"> <asp:textbox id="TextBox1" runat="server" Width="144px"> </asp:textbox> </TD>
<TD align="center"> <asp:button id="btnSubmit" onclick="Check_word1()" runat="server" Height="24px" Width="56px"
Text="Check"> </asp:button> </TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
--------------------编程问答-------------------- 对vb的语言不了解,但是我看出来俩点问题:
1.
function Check_word1()
var key1, word1;
key1 = "cat";
word1 = TextBox1.text;
var times = 0;
....
word1的值应该这么得到把: word1 = document.getElementById("TextBox1").value;
2,button有个属性 onClientClick的click事件,楼主应该不用
btnSubmit.Attributes.Add("onclick", "return Check_word1();")
难道和这句有关系?
--------------------编程问答-------------------- word1 = TextBox1.text;
javascript里的赋值? --------------------编程问答-------------------- <asp:button id="btnSubmit" onclick="Check_word1()" runat="server" Height="24px" Width="56px"
Text="Check"> </asp:button>
你的这个响应代码回传了,肯定在.cs文件里找不到,所以会报错 --------------------编程问答-------------------- <script language="vb" runat="server">
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Response.Write(" <script>Check_word1() </script>")
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnSubmit.Attributes.Add("onclick", "return Check_word1();")
End Sub
</script>
OnClick事件是在vb后台写的,应该还有个OnClientClick事件是调用客户端方法(js方法)
去掉:
btnSubmit.Attributes.Add("onclick", "return Check_word1();") button的默认事件只有click和clientclick
把下面的改成下面这样:
<asp:button id="btnSubmit" OnClientClick="Check_word1()" runat="server" Height="24px" Width="56px"
Text="Check">
应该会有个找不到对象的错误,自己继续找 我上面说了~
--------------------编程问答-------------------- 谢谢楼上的回答~
btnSubmit.Attributes.Add("onclick", "return Check_word1();")
这个是为了调用JavaScript 定义的那个功能
请问楼上,如果是因为回传的错误,那么应该怎么改呢?
补充:.NET技术 , ASP.NET