Visual Basic子过程问题

时间:2014-01-25 17:46:57

标签: asp.net visual-studio-2010 event-handling

我正在开发Visual Studio Web开发人员的一些.NET代码,但我遇到了问题。当我双击设计视图中的按钮而不是加载子过程时,它所做的只是突出显示:<asp:Button ID="btnEnter" runat="server" Text="Enter" onclick="btnEnter_Click" />

当我尝试运行它只是为了看看会发生什么,我收到此错误: Line 8: <asp:Button ID="btnEnter" runat="server" Compiler Error Message: BC30456: 'btnEnter_Click' is not a member of 'ASP.default_aspx'.

如果我删除onclick="btnEnter_Click"并运行它,它就可以了。无论哪种方式,当我双击按钮/元素时,它不应该为我创建一个类似的子程序吗? Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)

End Sub

我尝试输入那个manaully,但关键字不会变成蓝色或任何颜色,当我运行它时,它只是显示为webform上的文本与我的其他元素。以下是我到目前为止的所有内容:

<%@ Page Title="Home Page" Language="VB"%>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Enter a person's name below"></asp:Label>
<p>
    <asp:TextBox ID="txtStudentName" runat="server"></asp:TextBox>
</p>
<p>
    <asp:Button ID="btnEnter" runat="server"
       Text="Enter" onclick="btnEnter_Click" />
</p>
<p>
<asp:Button ID="btnDisplay" runat="server" Text="Display all and exit" />
</p>
<p>
    <asp:Label ID="lbl2" runat="server" Text=" "></asp:Label>
</p>
<p>
<asp:Label ID="lbl3" runat="server" Text=" "></asp:Label>
</p>
<p>
    <asp:Label ID="lbl4" runat="server" Text=" "></asp:Label>
</p>
<p>
    <asp:Label ID="lbl5" runat="server" Text=" "></asp:Label>
</p>
    </form>

编辑:

当我在default.aspx.vb文件中输入我的代码时,它会突出显示关键字等,但它不能引用我在设计视图中创建的元素。

Partial Class _Default     继承System.Web.UI.Page

结束班

Class Lab1
Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)

End Sub
Public Const length As Integer = 3
Shared counter2 As Integer = 0
Public Shared studentList As String() = New String(2) {}
Protected Sub btnEnter_Click(sender As Object, e As EventArgs)
    Label1.Text = "Enter a person's name"

    Dim studentName As [String] = txtStudentName.Text
    If studentList.Length <= length Then
        If txtStudentName.Text <> "" Then
            Dim match As [Boolean] = True
            Dim i As Integer = 0
            While counter2 >= i
                If studentList(i) IsNot Nothing Then
                    If studentList(i).ToUpper() = txtStudentName.Text.ToUpper() Then
                        match = False
                        Label1.Text = "This name has already been used"
                    End If
                End If
                i += 1
            End While
            If match = True Then
                studentList(counter2) = txtStudentName.Text
                counter2 += 1
            End If
        End If
    End If
End Sub
End Class

2 个答案:

答案 0 :(得分:2)

您的Page声明似乎表明您使用的是inline样式:

<%@ Page Title="Home Page" Language="VB"%>

这意味着您的代码/ {应该 aspx文件本身位于script runat="server"代码中

<%@ Page Language="VB" %>

<!DOCTYPE html> 

<script runat="server">

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        'Do something
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title></title>
</head>
<body>
   <form id="form1" runat="server">
      <div>    
          <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />    
      </div>
   </form>
</body>
</html>

但是,您的其他代码表明您似乎想要使用code-behind模型,其中包含.vb个文件(例如foo.aspx.vb

Page对于网站声明如此(对于网络应用,它会说CodeBehind="foo.aspx.vb"):

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="foo.aspx.vb" Inherits="foo" %>
  • 您会在项目中找到foo.asp.vb个文件
  • 它的类名将是您文件的名称(foo)
  • 您的代码应限定在此课程中(我不确定您帖子中的Class Lab1是为了什么......

foo.aspx.vb

Partial Class foo
   Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Do something
    End Sub

End Class

“修复”适合您:

  • 选择您要编写代码的模型:inlinecode-behind
  • 适当声明Page
  • 对于inline,您的代码将在aspx文件中
  • 对于code-behind,您的代码将存在,并且应该封装在页面的Class file内。
    • Page声明实际上是这样说的:
      • CodeFile="foo.aspx.vb"是代码
      • 的文件
      • Inherits="foo"是文件Partial Class
      • 中的Partial Class foo

答案 1 :(得分:0)

试试这个

Private Sub btnEnter_Click(sender As Object, e As System.EventArgs) Handles btnEnter.Click

End Sub

此处, btnEnter_Click 是您的事件名称,并在过程声明的末尾使用句柄关键字,以使其处理由对象变量引发的事件