无法从表单

时间:2018-02-14 00:56:10

标签: c# forms visual-studio web-services visual-studio-2017

我有一个网站,它接收电子邮件,运行powershell脚本,从该脚本获取输出并根据该输出动态创建按钮。我的麻烦在于,当您点击后退按钮并再次浏览时,旧按钮仍然保留,并且仍然在其上动态创建更多按钮,导致重复按钮,并且来自电子邮件地址的用户访问来自其他电子邮件地址。

但是,我不能弄清楚如何在页面改变后摆脱按钮。 我已尝试动态创建按钮到面板中,但Panel.Clear也不适用于我,也不是Panel.Dispose或Form.Dispose。

这是网站的流程: WebForm1>帐户(代码WebForm4)> WebForm2> WebForm3

Page 2 C#:

namespace ActiveDirectoryReset
{
public partial class WebForm4 : System.Web.UI.Page
{
    public static string user; //this comes from what is returned by first 
page
    public static string toEmail; //email from previous page
    public static string key; //irrelevant



    protected void Page_Load(object sender, EventArgs e)
    {



        int id = 1;
        int eventid = 0;




        //makes happy little buttons for each account associated with email 
provided//

        System.Web.UI.HtmlControls.HtmlForm form = form1;

        Controls.Add(form);

        foreach (var account in WebForm1.accounts)
            {


                eventid = id;
                id++;

                System.Web.UI.WebControls.Button button = new System.Web.UI.WebControls.Button();

                button.ID = "btn" + eventid;
                button.CssClass = "button";
                button.Click += new EventHandler(this.selectUser);
                button.Text = account;

                button.Font.Name = "Segoe UI";

                form.Controls.Add(button);


            }







    }
    //called after user is selected (dynamically created button is clicked)//

    public void selectUser(Object sender, EventArgs e)
    {
        System.Web.UI.WebControls.Button buttonClicked = (System.Web.UI.WebControls.Button)sender;
        user = buttonClicked.Text;
        WebForm2.hasBegun = true;

        generateKey();
        sendEmail();

        Server.Transfer("WebForm2.aspx");



    }



    //creates and sends email//
     public void sendEmail()
        {


            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add(toEmail);
            message.Subject = "Password Reset [[WORK IN PROGRESS]]";
            message.From = new System.Net.Mail.MailAddress("email@example.com");
            message.Body = "Hello, <br /><br />This is an email regarding your request to have your password changed for Active Directory account: " + user + ". If you did not request this, please disregard and contact your administrator. <br /><h3>Your key is:</h3><br /><h1>" + key + "</h1><br />To continue and reset your password, go back to the webpage and enter the key above.";


            message.IsBodyHtml = true;
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("xxxxxx");


            smtp.Send(message);


        }


    //generates key used to verity email//
    public void generateKey()
    {
        var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        var stringChars = new char[16];
        var random = new Random();

        for (int i = 0; i < stringChars.Length; i++)
        {
            stringChars[i] = chars[random.Next(chars.Length)];
        }
        key = new String(stringChars);
    }
//this is a cancel button that goes back to first page//
    protected void Button1_Click(object sender, EventArgs e)
    {
        WebForm2.hasBegun = false;



        Server.Transfer("WebForm1.aspx");
    }
}
}

2 个答案:

答案 0 :(得分:0)

在页面加载事件中,尝试添加代码以删除/删除按钮类型的每个控件,然后再添加新的动态按钮。

答案 1 :(得分:0)

我发现了这个问题。

按钮是根据第1页上的powershell脚本的输出创建的。输出被放入一个静态的列表中,因此帐户不会被删除,并且列表会变得更长。

解决方案:

protected void Button1_Click(object sender, EventArgs e){ WebForm1.accounts.RemoveAll(old => old.Contains("")); Server.Transfer("WebForm1.aspx");