我目前正在学习使用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>
我尝试将“Onclick =”submit“添加到asp.net代码中,但它仍然存在错误 任何帮助将非常感谢。 感谢您的时间和帮助。
答案 0 :(得分: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" />
答案 1 :(得分: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