我试图通过将按钮控件添加到设计器中包含的占位符来将其放置在页面上。原始表单的占位符中包含的按钮可以正常工作,但是在按钮选择事件之后添加的按钮不起作用。属性(即文本)可以正常工作,但与按钮关联的事件不会触发。
using System;
using System.Web.UI.WebControls;
namespace WebTrial
{
public partial class Login : System.Web.UI.Page
{
Boolean SignUp = false;
protected void Page_Load(object sender, EventArgs e)
{
SetUpControls();
}
public void SetUpControls()
{
Button butSubmit = new Button(), butSignIn = new Button(), butSignUp = new Button();
butSignIn.ID = "butSignIn";
butSignUp.ID = "butSignUp";
butSubmit.Text = "SUBMIT";
butSubmit.Click += new EventHandler(butSubmit_Click);
butSignIn.Text = "Sign In";
butSignIn.Click += new EventHandler(butSignIn_Click);
butSignUp.Text = "Sign Up";
butSignUp.Click += new EventHandler(butSignUp_Click);
if (!SignUp)
{
PlaceHolder1.Controls.Clear();
PlaceHolder2.Controls.Clear();
PlaceHolder1.Controls.Add(butSubmit);
PlaceHolder2.Controls.Add(butSignUp);
}
else
{
PlaceHolder1.Controls.Clear();
PlaceHolder2.Controls.Clear();
PlaceHolder1.Controls.Add(butSubmit);
PlaceHolder2.Controls.Add(butSignIn);
}
}
protected void butSignIn_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("SignInHit"); //Text does not come through
SignUp = false;
SetUpControls();
}
protected void butSignUp_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("SignUpHit"); //Text does come through
SignUp = true;
SetUpControls();
}
protected void butSubmit_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("SubmitHit"); //Works on the Sign In view but not the Sign Up
}
}
}