使用母版页将C#添加到webform

时间:2013-11-24 19:03:30

标签: c# html button

我想添加我的代码以通过C#发送电子邮件,但我的按钮似乎不起作用。

HTML代码:

<div style="text-align:center">
    <p style="background-color: Yellow">if you have a video that you think should be here send it to us</p>Name:
    <input type="text" id="Name" />Link:
    <input type="text" id="Link" />
    <br />Why you think this video should be here:
    <br />
    <textarea id="Why" rows="5" cols="50"></textarea>
    <br />
    <input type="submit" id="submit" name="submit" value="Send" style="height: 25px; width: 200px" />
</div>

aspx.cs代码:

public partial class Videos : System.Web.UI.Page
{
    public string Answer;
    protected void Page_Load(object sender, EventArgs e)
    {
        Answer = "Email Sent2";
        if (Request.Form["submit"] != null)
        {
            Answer = "Email Sent3";
            string Name = Request.Form["Name"];
            string Link = Request.Form["Link"];
            string Why = Request.Form["Why"];
            string body = "<div dir='ltr'>";
            body += ("<h4>You got a new video idea from </h4>");
            body += (Name);
            body += ("<br />");
            body += ("Link: " + Link);
            body += ("<br />");
            body += ("Reason: " + Why);
            body += ("</div>");

            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("MY EMAIL", "MY PASS"),
                EnableSsl = true
            };
            System.Net.Mail.MailMessage mail1 = new System.Net.Mail.MailMessage();
            mail1.Body = body;
            mail1.From = new System.Net.Mail.MailAddress("MY EMAIL");
            mail1.IsBodyHtml = true;
            mail1.Subject = "Vidoe Idea By " + Name;
            mail1.To.Add("MY EMAI");

            client.Send(mail1);
            Answer = "Email Sent";
        }
    }

我不知道为什么,但是当我按下按钮时没有发生任何事情。 这有什么问题?

2 个答案:

答案 0 :(得分:0)

如果您使用的是网络表单,则最好使用网络表单控件。要素:

<input type="text" id="Name" />Link:
<input type="text" id="Link" />

没有runat =“server”标记和id,这是在服务器端代码中读取这些输入的内容所必需的。

然后您必须使用asp:按钮然后分配一个事件来运行发送电子邮件代码。如果您使用此事件页面加载,如果检查是否回帖,则可以获得此代码发送的大量电子邮件。

我建议创建一个新的Web表单,为您的输入添加asp:textbox,输入的asp:按钮在按钮中创建事件并在那里添加代码。使用text属性读取内容并调试代码。

答案 1 :(得分:0)

您应该对Webforms在ASP.net中的工作方式进行更多研究。

Webforms旨在与服务器控件协同工作,您可以在VisualStudio的工具栏中找到它们。请求对象仍然有效,并且是webforms的支柱。但请注意,请求对象需要name属性。就HTML对象而言,ID用于客户端,name用于服务器端。这是一个过于简单化但有用的指南。

Webforms也非常注重事件,您应该利用这一事实并使用按钮的点击事件。

我会使用以下内容:

<强> HTML / ASPX

<div style="text-align:center">
    <p style="background-color: Yellow">if you have a video that you think should be here send it to us</p>
    <!-- Use Label to create a label associated with the control
    This is good practice -->
    <asp:Label id="lblName" runat="server" AssociatedControlID="txtName" EnableViewState="false">Name:</asp:Label>
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    <br /><!-- Not really best practice to use <br /> for formatting, use CSS instead --> 
    <asp:Label ID="lblLink" runat="server" EnableViewState="false" AssociatedControlID="txtLink">Link</asp:Label>
    <asp:TextBox ID="txtLink" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="lblWhy" runat="server" EnableViewState="false" AssociatedControlID="txtWhy">Why you think this video should be here:</asp:Label>
    <br />
    <asp:TextBox ID="txtWhy" TextMode="MultiLine" Rows="5" Columns="50" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="btnSubmit" runat="server"  Text="Send" 
           style="height: 25px; width: 200px" onclick="btnSubmit_Click"/>
<!-- A Message to display when email has been sent -->
<asp:Panel ID="pnlSucess" runat="server" Visible="false">Your feed back is appreciated!</asp:Panel>
</div>

<强> .aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    //Note: Nothing Here... 
    //Use this event for setting the page up
}

//Use this event for handling the click
protected void btnSubmit_Click(object sender, EventArgs e)
{
    //MAKE SURE YOU USE ERROR HANDLING FOR THE REAL WORLD

    string Answer = string.Empty;
    Answer = "Email Sent3";
    StringBuilder builder = new StringBuilder();

    //NOTE: You should be sanitising client input
    string Name = txtName.Text;
    string Link = txtLink.Text;
    string Why = txtWhy.Text;

    //String builder is a better way to build a string
    //or look at string.Format();            
    builder.Append("<div dir='ltr'>");
    builder.AppendLine("<h4>You got a new video idea from </h4>");
    builder.AppendLine(Name);
    builder.AppendLine("<br />");
    builder.AppendLine("Link: ");
    builder.Append(Link);
    builder.AppendLine("<br />");
    builder.AppendLine("Reason: ");
    builder.Append(Why);
    builder.AppendLine("</div>");

    var client = new SmtpClient("smtp.gmail.com", 587)
    {
        Credentials = new NetworkCredential("MY EMAIL", "MY PASS"),
        EnableSsl = true
    };
    System.Net.Mail.MailMessage mail1 = new System.Net.Mail.MailMessage();
    mail1.Body = builder.ToString(); //Get the body
    mail1.From = new System.Net.Mail.MailAddress("MY EMAIL");
    mail1.IsBodyHtml = true;
    mail1.Subject = "Vidoe Idea By " + Name;
    mail1.To.Add("MY EMAI");

    client.Send(mail1);
    Answer = "Email Sent";

    //Display a message so the user knows it worked!
    pnlSucess.Visible = true;
}

确保将using System.Net;添加到后面代码顶部的using语句中。这是StringBuilder

所必需的