简体   繁体   English

如何在同一项目中从aspx.cs调用方法到另一个aspx.cs?

[英]How to call a method from aspx.cs to another aspx.cs in same project?

I have 2 aspx.cs pages. 我有2个aspx.cs页面。 First page is for login, after login i call to method which send and email as alert that a user loged in. After log in you move to second page. 第一页用于登录,登录后我调用发送和电子邮件作为提醒用户登录的方法。登录后,您将移至第二页。 I want to create another button in the second page, when you press it, it will send email again (the same sending method from page 1). 我想在第二页中创建另一个按钮,当您按下它时,它将再次发送电子邮件(与第1页中的发送方法相同)。 Can i in page 2, call to the "send" method from page 1 or i must write the entire method in each page? 我可以在第2页中从第1页调用“发送”方法,还是必须在每页中编写整个方法?

This is page 1 code behind: 这是后面的第1页代码:

public partial class UserLogin : System.Web.UI.Page
{
private string _employee;
private string _userName;
private string _loginDatetime;

protected void Page_Load(object sender, EventArgs e)
{

}

protected void LoginBtn_Click(object sender, EventArgs e)
{
    _employee = employeeBox.Text;
    _userName = userBox.Text;
    _loginDatetime = DateTime.Now.ToString();

    SendMail("your_email@gmail.com", "your_email@gmail.com", "your_email@gmail.com", "Login Alert",
        "Hello " + _employee + ",<Br>User \"" + _userName + "\" just log to the website on the following date: " + DateTime.Now);

    Response.Redirect("http://localhost:21361/UserDeposit.aspx?_employee=" + employeeBox.Text + "&_userName=" + userBox.Text + "&_loginDatetime=" + _loginDatetime);
}

protected string SendMail(string toList, string from, string ccList, string subject, string body)
{

    MailMessage message = new MailMessage();
    SmtpClient smtpClient = new SmtpClient();
    string msg;
    try
    {
        MailAddress fromAddress = new MailAddress(from);
        message.From = fromAddress;
        message.To.Add(toList);
        if (!string.IsNullOrEmpty(ccList))
            message.CC.Add(ccList);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        smtpClient.Host = "smtp.gmail.com";   
        smtpClient.Port = 587;
        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new NetworkCredential("your_email@gmail.com", "password");

        smtpClient.Send(message);
        msg = "Successful";
        Response.Write("<script>alert('" + msg + "')</script>");
    }
    catch (Exception ex)
    {
        msg = ex.Message;
        Response.Write("<script>alert('" + msg + "')</script>");
    }
    return msg;
}
}

This is page 2 code behind: 这是后面的第2页代码:

public partial class UserDeposit : System.Web.UI.Page
{
string _employeeName;
string _userName;
string _loginTime;
DateTime time;

protected void Page_Load(object sender, EventArgs e)
{
    _employeeName = Request.QueryString["_employee"];
    EmployeeName.Text = _employeeName;

    _userName = Request.QueryString["_userName"];
    UserName.Text = _userName;

    _loginTime = Request.QueryString["_loginDatetime"];
    time = DateTime.Parse(_loginTime);
}



protected void Button1_Click(object sender, EventArgs e)
{
    //call here the "send mail" method??
}
}

What about 3: You do not have the send method in any page but put it in a separate class. 怎么样3:您在任何页面中都没有send方法,但是将其放在单独的类中。

That is proper programming practice. 那是正确的编程习惯。

You need to do either one of the below methods: 您需要执行以下任一方法:

  1. Create a base Page class and inherit that base page class in all appropriate aspx pages. 创建一个基类,并在所有适当的aspx页中继承该基类。 (preferred method) (首选方法)

  2. Create a static class and put them there. 创建一个静态类,并将其放在那里。

You make the SendMail method static like this: 您可以像这样使SendMail方法static

public static string SendMail(string toList, string from, string ccList, string subject, string body)

Now you can call it from anywhere, just append the class name in front, like this: 现在您可以从任何地方调用它,只需将类名附加在前面,如下所示:

UserLogin.SendMail(toList, from, ccList, subject, body);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM