【问题标题】:Adding C# to webform with master page使用母版页将 C# 添加到 webform
【发布时间】:2013-12-09 09:09:44
【问题描述】:

我想添加我的代码以通过 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";
        }
    }

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

【问题讨论】:

  • 您的键盘有问题吗?尝试使用 asp:button 控件而不是 HTML 输入
  • 首先,调试您的代码,因为“我按下按钮没有发生任何事情”是用户问题,而不是程序员问题。这里是关于编程问题。其次,我不知道您是否可以使用 webforms 阅读简单的 HTML,为什么不使用 并在 C# 中将其作为 txtName.Text 阅读而不是请求。形式?所以,调试你的代码,搜索它不工作的地方,然后再告诉我们。
  • 我调试了我的代码。直到 if(Request.Form..
  • 或者,为了区分第一次提交或加载(我怀疑你正在尝试这样做),只需使用IsPostBack
  • 您似乎错过了一些网络表单的要点。您是来自 PHP 还是经典 ASP 背景?也许 ASP.net MVC 会更适合你。另请查看StringBuilder 以替换您正在执行的sting concatination。

标签: c# html button


【解决方案1】:

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

Webforms 旨在与服务器控件协同工作,您可以在 VisualStudio 的工具栏中找到这些控件。请求对象仍然可以工作,并且是 Web 表单的主干。但请注意,请求对象需要 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 所必需的。

【讨论】:

    【解决方案2】:

    如果您使用网络表单,您最好使用网络表单控件。元素:

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

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

    您必须使用 asp:button 然后分配一个事件来运行发送电子邮件代码。如果您使用此事件页面加载而不使用 if 检查它是否是回发邮件,则可能会收到此代码发送的大量电子邮件。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多