我正在使用一个使用网站主文件的asp
内容页面。我对带有标签的runat=server
和让vb.net
执行感到困惑。我试过了,但是没用:
<div class="card-body">
<asp:Label ID="Label1" runat="server" />
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" aria-describedby="emailHelp" placeholder="Enter name" required>
</div>
<div class="form-group" runat="server">
<asp:Label for="email" runat="server" Text="Email address"></asp:Label>
<input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email" required>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" rows="6" required></textarea>
</div>
<div class="mx-auto">
<asp:Button type="submit" class="btn btn-primary text-right" ID="Btn_SendMessage" runat="server" Text="Submit"></asp:Button>
</div>
我尝试将html
代码放入表单中,但错误提示不能嵌套表单,因此对于如何将html
发送到vb.net
代码感到困惑
VB.NET
代码:
Protected Sub Btn_SendMessage_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim Email As String = FindControl("email").ToString
Dim Name As String = FindControl("name").ToString
Dim Message As String = FindControl("message").ToString
Dim Mail As New MailMessage
Dim SMTP As New SmtpClient("smtp.gmail.com")
Mail.Subject = Name
Mail.From = New MailAddress(Email)
SMTP.Credentials = New System.Net.NetworkCredential("xxxx@gmail.com", "xxxxxxxx") '<-- Password Here
Mail.To.Add("xxxxxxx@gmail.com")
Mail.Body = Message
SMTP.EnableSsl = True
SMTP.Port = "587"
Try
SMTP.Send(Mail)
Label1.Text = "Message sent"
Catch ex As Exception
Label1.Text = ex.ToString
End Try
End Sub
页面刷新,没有任何反应,我什至不认为VB.NET
被执行。
答案 0 :(得分:0)
以下是基于您的代码的有效示例:
ASPX
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div class="card-body">
<asp:Label ID="Label1" runat="server" />
<div class="form-group">
<label for="name">Name</label>
<asp:Label ID="Label4" runat="server" AssociatedControlID="TextBoxName" Text="Name"></asp:Label>
<asp:TextBox ID="TextBoxName" runat="server" CssClass="form-control" placeholder="Enter name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ErrorMessage="*" ControlToValidate="TextBoxName"></asp:RequiredFieldValidator>
</div>
<div class="form-group" runat="server">
<asp:Label ID="Label3" runat="server" AssociatedControlID="TextBoxEmail" Text="Email address"></asp:Label>
<asp:TextBox ID="TextBoxEmail" runat="server" CssClass="form-control" placeholder="Enter name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEmail" runat="server" ErrorMessage="*" ControlToValidate="TextBoxEmail"></asp:RequiredFieldValidator>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<asp:Label ID="Label2" runat="server" AssociatedControlID="TextBoxMessage" Text="Message"></asp:Label>
<asp:TextBox ID="TextBoxMessage" runat="server" TextMode="MultiLine" CssClass="form-control" Rows="6"></asp:TextBox>
</div>
<div class="mx-auto">
<asp:Button type="submit" class="btn btn-primary text-right" ID="Btn_SendMessage" runat="server" Text="Submit" OnClick="Btn_SendMessage_Click"></asp:Button>
</div>
</div>
</form>
</body>
</html>
背后的代码
Protected Sub Btn_SendMessage_Click(sender As Object, e As EventArgs) Handles Btn_SendMessage.Click
Dim Mail As New MailMessage
Dim SMTP As New SmtpClient("smtp.gmail.com")
Mail.Subject = TextBoxName.Text
Mail.From = New MailAddress(TextBoxEmail.Text)
SMTP.Credentials = New System.Net.NetworkCredential("xxxx@gmail.com", "xxxxxxxx") '<-- Password Here
Mail.To.Add("xxxxxxx@gmail.com")
Mail.Body = TextBoxMessage.Text
SMTP.EnableSsl = True
SMTP.Port = "587"
Try
SMTP.Send(Mail)
Label1.Text = "Message sent"
Catch ex As Exception
Label1.Text = ex.ToString
End Try
End Sub
具有runat="server"
属性的标记是ASP.NET服务器端控件,它们在发送到浏览器的结果网页中以HTML副本的形式呈现。在页面的服务器端处理期间,您可以直接使用它们的ID在代码中访问它们。
请注意,我已经在必填字段中使用了服务器端验证控件。如果您收到有关非侵入式验证的运行时错误,则可能需要在Web.config
文件中添加以下设置:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
希望有帮助。我正在使用Visual Studio 2017,目标框架是.NET 4.6.1。