我有一个网站,该网站接收电子邮件,运行powershell脚本,从该脚本中获取输出,并根据该输出动态创建按钮。我的麻烦是,当您键入后按钮并再次浏览它时,旧按钮仍然保留,并且在此基础上仍然动态创建更多按钮,从而产生重复的按钮,以及来自电子邮件地址的用户从其他电子邮件地址。
但是,一旦页面更改,我就无法弄清楚如何摆脱按钮。我已经尝试将按钮动态地创建到面板中,但是面板。Clear也不适合我,面板也不适用。dispose或form.dispose。
这是网站的流程:WebForm1>帐户(在代码WebForm4(> WebForm2> WebForm3
第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");
}
}
}
在您的页面加载事件上,尝试在添加新的动态按钮之前添加代码删除/删除按钮类型的每个控件。
我找到了问题。
根据第1页的PowerShell脚本的输出创建按钮。将输出放入静态的列表中,因此未删除帐户,并且列表越来越长。
解决方案:
protected void Button1_Click(object sender, EventArgs e){
WebForm1.accounts.RemoveAll(old => old.Contains(""));
Server.Transfer("WebForm1.aspx");