簡體   English   中英

如何在同一項目中從aspx.cs調用方法到另一個aspx.cs?

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

我有2個aspx.cs頁面。 第一頁用於登錄,登錄后我調用發送和電子郵件作為提醒用戶登錄的方法。登錄后,您將移至第二頁。 我想在第二頁中創建另一個按鈕,當您按下它時,它將再次發送電子郵件(與第1頁中的發送方法相同)。 我可以在第2頁中從第1頁調用“發送”方法,還是必須在每頁中編寫整個方法?

這是后面的第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;
}
}

這是后面的第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??
}
}

怎么樣3:您在任何頁面中都沒有send方法,但是將其放在單獨的類中。

那是正確的編程習慣。

您需要執行以下任一方法:

  1. 創建一個基類,並在所有適當的aspx頁中繼承該基類。 (首選方法)

  2. 創建一個靜態類,並將其放在那里。

您可以像這樣使SendMail方法static

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

現在您可以從任何地方調用它,只需將類名附加在前面,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM