vb.net 如何在asp.net.vb 中提交按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15432082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:48:54  来源:igfitidea点击:

How submit a button in asp.net.vb?

asp.netvb.net

提问by user1574185

I am currently learning to program in asp.net and I am writing to seek help on the following issue below:

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

I am trying to execute the following connection below but I when click the button connect in the browser, nothing happens.

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

aspx.vb code:

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 code:

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>

I tried adding "Onclick = "submit" to the asp.net code but it kept through an error.
Any help would be very much appreciated. Thank you for your time and help.

我尝试在 asp.net 代码中添加“Onclick =”submit”,但它仍然通过一个错误。
任何帮助将不胜感激。感谢您的时间和帮助。

回答by Joel Etherton

There are two ways to wire up this button event. The first would be in code behind by modifying your signature to add the Handleskeyword:

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

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

The second would be in the html side by defining the onClick event to be assigned to the function itself

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

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

回答by pala?н

Just modify your VB.Net code like this using Handles:

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

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
     ' Your code here
End Sub