2

我目前正在学习在 asp.net 中编程,我正在写信寻求以下问题的帮助:

我正在尝试执行以下连接,但是当我单击浏览器中的连接按钮时,没有任何反应。

aspx.vb 代码:

  Imports System.Data
  Imports System.Data.SqlClient
  Imports System.Configuration

Partial Class _Default
Inherits System.Web.UI.Page

' Public Sub Button1(ByVal sender As Object, ByVal e As System.EventArgs)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim connectionString As String
    Dim connection As SqlConnection
    connectionString = ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString
    connection = New SqlConnection(connectionString)
    Try
        connection.Open()
    Catch ex As Exception
        MsgBox("Not working")
    End Try
    If connection.State = 1 Then
        MsgBox(" working")
    End If
End Sub
End Class

asp.net 代码:

 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

  <!DOCTYPE html>

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


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

我尝试在 asp.net 代码中添加“Onclick =”submit”,但它一直出现错误。
非常感谢您的帮助。感谢您的时间和帮助。

4

2 回答 2

6

有两种方法可以连接此按钮事件。第一个将通过修改您的签名以添加Handles关键字在代码后面:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

第二个将在 html 端通过定义要分配给函数本身的 onClick 事件

<asp:Button ID="Button1" runat="server" Text="Button" onClick="Button1_Click" />
于 2013-03-15T12:11:21.803 回答
2

只需使用Handles像这样修改您的 VB.Net 代码:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ' Your code here
End Sub
于 2013-03-15T12:16:59.400 回答