List<string> myControls = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
myControls = new List<string>();
ViewState["myControls"] = myControls;
}
}
protected void override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
myControls = (List<string>)ViewState["myControls"];
foreach(string controlID in myControls){
//method to create your buttons goes here.
createButtons(controlID);
}
}
public void createButtons(string btnID){
Button btn = new Button();
btn.ID = btnID;
btn.Text = "Add New";
btn.Click += new RoutedEventHandler(this.GreetingBtn_Click);
Panel1.Controls.Add(btn);
Panel1.Controls.Add(new LiteralControl("<br /><br />"));
}
void GreetingBtn_Click(Object sender, RoutedEventArgs e){
create();
}
试试这个代码。在 asp.net 中动态添加控件时,您必须在 postback 中重新创建控件。最简单的方法是使用上面显示的视图状态。
未正确执行此操作的常见迹象是:单击时控件消失或事件根本不触发。希望这对某人有帮助!