简体   繁体   中英

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

I have 2 aspx.cs pages. 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). Can i in page 2, call to the "send" method from page 1 or i must write the entire method in each page?

This is page 1 code behind:

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:

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.

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. (preferred method)

  2. Create a static class and put them there.

You make the SendMail method static like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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