如何在不使用表单的情况下在C#中使用ASP.NET创建消息表单

时间:2013-08-12 02:03:28

标签: c# asp.net forms nested

好的,这就是我到目前为止所拥有的。我不知道在哪里放我的电子邮件地址,也不知道有什么错误。它不断传达信息。

我的content.aspx页面:

<%@ Page Title="Contact" Language="C#" MasterPageFile="~/Site.Master"  AutoEventWireup="true" CodeBehind="Contact.aspx.cs" Inherits="JeremiahTorresportfoliov1.Contact"%>


<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

<hgroup class="title">
    <h2>How to get in touch</h2>
</hgroup>

<section class="contact">
    <header>
        <h3>Phone:</h3>
    </header>
    <p> 
        <span>863.307.1652</span>
    </p>    
</section>
<section class="contact">
    <header>
        <h3>Email:</h3>
    </header>
    <p>
        <span><a href="lostdestany2685@gmail.com">Jeremiah@Leftclique.com</a></span>
    </p>
</section>

<section class="contact">
    <header>
        <h3>Address:</h3>
    </header>
    <p>
        5337 N. Socrum Loop Rd. #208<br />
        Lakeland, FL 33809
    </p>

</section>


<%@ Register Src="~/MyUserControl.ascx" TagPrefix="uc1" TagName="MyUserControl" %>



<uc1:MyUserControl runat="server" id="MyUserControl" />
</asp:Content>

这是我的contact.aspx.cs:

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;

namespace JeremiahTorresportfoliov1
{
public partial class Contact : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
}

MyUserControl.aspx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="JeremiahTorresportfoliov1.MyUserControl" %>

<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="Other">Other</asp:ListItem>
</asp:DropDownList>
</div>
<ul>

    <asp:Label EnableViewState="false" ID="lblErrorMessage" runat="server"></asp:Label>

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

MyUserControl.ascx.cs

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;

namespace JeremiahTorresportfoliov1{
public class MyUserControl : 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;



    //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 = "lostdestany@aol.com";
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.";

}


}

1 个答案:

答案 0 :(得分:0)

您无法从控件继承表单,它应该来自System.Web.UI.Page。添加usercontrol并将所有标记和代码移动到该控件:

<强> MyUserControl.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="JeremiahTorresportfoliov1.MyUserControl" %>

<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="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>

<强> MyUserControl.ascx.cs:

namespace JeremiahTorresportfoliov1
    public partial class MyUserControl : 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 = "info@example.com.au";
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.";

}


    }
}

将此控件添加到您的页面并修复代码。

<强> Contact.aspx:

<%@ Page Title="Contact" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Contact.aspx.cs" Inherits="JeremiahTorresportfoliov1.Contact" %>

<%@ Register Src="~/MyUserControl.ascx" TagPrefix="uc1" TagName="MyUserControl" %>


<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <uc1:MyUserControl runat="server" id="MyUserControl" />
</asp:Content>

<强> Contact.aspx.cs:

namespace JeremiahTorresportfoliov1
{
    public partial class Contact : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

更新:我在您的更新中看到您仍然有错误。在MyUserControl.ascx.cs中更改此内容:

public class MyUserControl : System.Web.UI.UserControl

到此:

public partial class MyUserControl : System.Web.UI.UserControl

在Contact.aspx中,将对Myusercontrol的引用移到页面顶部,如下所示:

<%@ Page Title="Contact" Language="C#" MasterPageFile="~/Site.Master"  AutoEventWireup="true" CodeBehind="Contact.aspx.cs" Inherits="JeremiahTorresportfoliov1.Contact"%>

<%@ Register Src="~/MyUserControl.ascx" TagPrefix="uc1" TagName="MyUserControl" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">