我在表單上有以下代碼,以便用戶提交表單併發送併發送電子郵件。我有兩個response.write語句,我想改變它們,以便它將這些寫入標籤(lblSubmit)。任何幫助,將不勝感激。回覆。寫給標籤
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SmtpClient sc = new SmtpClient("smtp.talktalk.net");
StringBuilder sb = new StringBuilder();
MailMessage msg = null;
sb.Append("Email from: " + txtEmail.Text + "\n");
sb.Append("Message : " + txtQuestion.Text + "\n");
try
{
msg = new MailMessage(txtEmail.Text,
"[email protected]", "Message from Web Site",
sb.ToString());
sc.Send(msg);
Response.Write("Message sent!");
}
catch (Exception ex)
{
// something bad happened
Response.Write("Something bad happened!");
}
finally
{
if (msg != null)
{
msg.Dispose();
}
}
}
}
</script>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div id="title"><p>Contact Us...</p></div>
<div id="titletext"><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p></div>
<div id="contactform">
<asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
<asp:TextBox ID="txtName" runat="server" Width="189px"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtName" ErrorMessage="Please enter your name"
ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator>
<br /><br />
<asp:Label ID="lblEmail" runat="server" Text="Email:"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server" Width="189px"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtEmail" Display="Dynamic" ErrorMessage="Please enter an email address"
ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtEmail" ErrorMessage="Please enter a valid Email address"
ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Display="Dynamic"></asp:RegularExpressionValidator>
<br />
<br />
<asp:Label ID="lblPhone" runat="server" Text="Phone:"></asp:Label>
<asp:TextBox ID="txtPhone" runat="server" Width="189px"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="txtPhone"
ErrorMessage="Please enter a contact telephone number" ForeColor="Red"
Display="Dynamic"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="lblQuestion" runat="server" Text="Question:"></asp:Label>
<asp:TextBox ID="txtQuestion" runat="server" Height="103px" TextMode="MultiLine"
Width="189px"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtQuestion" ErrorMessage="Please enter a message"
ForeColor="Red"></asp:RequiredFieldValidator>
<asp:Label ID="lblSubmit" runat="server"></asp:Label>
<asp:Button ID="cmdSubmit" runat="server" Text="Submit" CssClass="cmdSubmit"
onclick="cmdSubmit_Click" PostBackUrl="~/Contact Us.aspx" />
</div>
</asp:Content>
謝謝你:) – user1300580 2012-04-05 01:00:05
嗨,catch語句是我正在尋找和任何幫助如何實際顯示錯誤消息,而不是評論「發生了什麼不幸的事情」將不勝感激。 Thankyou – user1300580 2012-04-05 01:06:56
@ user1300580:看看你正在捕獲的'ex'對象:http://msdn.microsoft.com/en-us/library/system.exception.aspx它包含大量有用的信息,最具體的'.Message'(錯誤消息)和'.StackTrace'(發生錯誤的位置)。需要注意的一點是,當值違反應用程序的邏輯時,您可以創建並拋出自定義異常。例如,如果你有一個帶'int'參數的函數,並且'int' _should_是'> 0',那麼你可以說'throw new ArgumentException(「Value must be a integer integer。」);'在函數中。 – David 2012-04-05 01:10:33