我想通过一个简单的联系表格来完成基本功能,并将表格电子邮件提交给某人。这在 asp.net 中很容易做到,但是一旦我将它作为用户控件上传,我就遇到了麻烦。你有一个很好的例子我可以看看吗?谢谢!
最佳答案
它与普通asp.net页中的情况相同,该示例假定您使用的是最新版本的Sitefinity,并且您的母版页上具有RadScriptManager或ScriptManager。
首先,这是我的示例代码背后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
using System.ComponentModel;
public partial class UserControls_LandingPage_contactForm : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
bool bSent = false;
try
{
//create the email and add the settings
var email = new MailMessage();
email.From = new MailAddress(FromEmail);
email.To.Add(new MailAddress(FromEmail));
email.Subject = Subject;
email.IsBodyHtml = true;
//build the body
var sBody = new StringBuilder();
sBody.Append("<strong>Contact Details</strong><br /><br />");
sBody.AppendFormat("Needs: {0}<br />", cboConsultationType.SelectedValue);
sBody.AppendFormat("Name: {0}<br />", txtName.Text);
sBody.AppendFormat("Email: {0}<br />", txtEmail.Text);
sBody.AppendFormat("Number: {0}<br />", txtPhone.Text);
sBody.AppendFormat("Comment: {0}<br />", txtMsg.Text);
email.Body = sBody.ToString();
//send the email
var smtpServer = new SmtpClient();
smtpServer.Send(email);
//mark as sent ok
bSent = true;
}
catch (Exception ex)
{
//send any errors back
//add your own custom handling of errors;
}
//let the end user know if it was a success
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('" + (bSent ? SuccessText : FailureText) + "');", true);
}
//properties
public string FromEmail
{
get { return _fromEmail; }
set { _fromEmail = value; }
}
public string Subject
{
get { return _subject; }
set { _subject = value; }
}
public string SuccessText
{
get { return _successText; }
set { _successText = value; }
}
public string FailureText
{
get { return _failureText; }
set { _failureText = value; }
}
//fields
private string _fromEmail = "[email protected]";
private string _subject = "Website Enquiry";
private string _successText = "Thank you for submitting your details we will be in touch shortly.";
private string _failureText = "There was a problem submitting your details please try again shortly.";
}
ASCX代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactForm.ascx.cs" Inherits="UserControls_LandingPage_contactForm" %>
<fieldset>
<div class="focus">
<label>
I need...</label>
<asp:DropDownList ID="cboConsultationType" runat="server" CssClass="select sub web">
<asp:ListItem Value="I Need A New Web Site">A completely new website</asp:ListItem>
<asp:ListItem Value="Web Site Upgrade">My website upgraded</asp:ListItem>
<asp:ListItem Value="Application Design">An application </asp:ListItem>
<asp:ListItem Value="An ecommerce website">New Ecommerce website</asp:ListItem>
<asp:ListItem Value="Other">Other</asp:ListItem>
</asp:DropDownList>
</div>
<ul>
<li>
<asp:Label EnableViewState="false" ID="lblErrorMessage" runat="server"></asp:Label>
</li>
<li>
<asp:Label EnableViewState="false" ID="lblName" AssociatedControlID="txtName" runat="server"
Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server" ValidationGroup="ContactValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtName" ErrorMessage="Name is required">*</asp:RequiredFieldValidator>
</li>
<li>
<asp:Label ID="lblPhone" runat="server" AssociatedControlID="txtPhone" EnableViewState="false"
Text="Phone"></asp:Label>
<asp:TextBox ID="txtPhone" runat="server" ValidationGroup="ContactValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorPhone" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtPhone" ErrorMessage="Phone is required">*</asp:RequiredFieldValidator>
</li>
<li>
<asp:Label ID="lblEmail" runat="server" AssociatedControlID="txtEmail" EnableViewState="false"
Text="Email"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server" ValidationGroup="ContactValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ValidationGroup="ContactValidation"
ControlToValidate="txtEmail" ErrorMessage="Email is required">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorEmail" runat="server"
ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="ContactValidation" ErrorMessage="Email address is invalid">*</asp:RegularExpressionValidator>
</li>
<li>
<asp:Label ID="lblMsg" runat="server" AssociatedControlID="txtMsg" EnableViewState="false"
Text="How can we assist you?"></asp:Label>
<asp:TextBox ID="txtMsg" runat="server" TextMode="MultiLine" Rows="5" Wrap="true"></asp:TextBox>
</li>
<li>
<asp:Button ID="btnSubmit" runat="server" EnableViewState="false" CssClass="submit"
Text="Send" ValidationGroup="ContactValidation" OnClick="btnSubmit_Click" />
</li>
</ul>
</fieldset>
然后,您需要警惕的唯一其他项目是在web.config中,您需要修改电子邮件的system.net设置:
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="smtp.yourdomain.com" userName="Your_Username" password="Your_Password" port="25" />
</smtp>
</mailSettings>
</system.net>
然后上传控件或修改您的web.config。然后,如果您的SMTP服务器都已正确设置,则表单应该不会出现任何问题。
我希望这能够帮到你。
关于c# - SiteFinity C#中的联系表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1099730/