我有一個動態頁面,其中包含一個包含內容的面板。我點擊按鈕添加一個新面板。使用動態添加面板中的按鈕,點擊事件不會觸發。動態創建的按鈕不會觸發其單擊事件
下面是表單代碼:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Panel ID="MainPanel" runat="server">
</asp:Panel>
</div>
</form>
這是創建一些面板和子按鈕(step_button
)代碼:
public partial class _Default : System.Web.UI.Page
{
Button button = new Button() { Text="Add level1"};
static int controls = 1;
private List<ActionPanel> PanelsList
{
get
{
if (Session["DataActionsPanels"] == null)
Session["DataActionsPanels"] = new List<ActionPanel>();
return (List<ActionPanel>)Session["DataActionsPanels"];
}
}
protected void Page_Load(object sender, EventArgs e)
{
button.Click += new EventHandler(button_Click);
button.Attributes.Add("style", "float:right;");
button.ID = "button1";
MainPanel.Controls.Add(button);
if (IsPostBack)
{
RestorePanels();
return;
}
AddActionPanel();
}
protected void RestorePanels()
{
foreach (ActionPanel panel in PanelsList)
AddActionPanel(panel);
}
protected void button_Click(object sender, EventArgs e)
{
controls++;
AddActionPanel();
}
protected void AddActionPanel()
{
var panel = new ActionPanel(controls, ScriptManager1);
PanelsList.Add(panel);
var button_index = MainPanel.Controls.IndexOf(button);
MainPanel.Controls.AddAt(button_index, panel.GetPanel());
}
protected void AddActionPanel(ActionPanel panel)
{
var button_index = MainPanel.Controls.IndexOf(button);
MainPanel.Controls.AddAt(button_index, panel.GetPanel());
}
}
public class ActionPanel
{
private Panel main_panel = new Panel();
private TextBox data_action_id_box = new TextBox();
private TextBox data_action_name_box = new TextBox();
private TextBox data_action_error_box = new TextBox() ;
Button step_button = new Button() { Text = "Add level2"};
private Panel steps_panel = new Panel();
private List<StepPanel> steps_list = new List<StepPanel>();
private int step_count = 1;
private ScriptManager _manager;
public ActionPanel(int i, ScriptManager manager)
{
_manager = manager;
main_panel.Attributes.Add("style", "float:left; width:100%");
main_panel.GroupingText = "TestLevel " + i;
main_panel.Controls.Add(new Label() { Text = "TestLevel1" });
data_action_id_box.Attributes.Add("style", "margin-left:13px");
main_panel.Controls.Add(data_action_id_box);
main_panel.Controls.Add(new Literal() { Text = "<br></br>" });
main_panel.Controls.Add(new Label() { Text = "TestLevel11" });
data_action_name_box.Attributes.Add("style", "margin-left:10px");
main_panel.Controls.Add(data_action_name_box);
main_panel.Controls.Add(new Literal() { Text = "<br></br>" });
main_panel.Controls.Add(new Label() { Text = "TestLevel11" });
data_action_error_box.Attributes.Add("style", "margin-left:33px");
main_panel.Controls.Add(data_action_error_box);
main_panel.Controls.Add(new Literal() { Text = "<br></br>" });
step_button.ID = "button2_"+i;
step_button.Click += new EventHandler(AddStep_Click);
step_button.Attributes.Add("style", "float:right;");
steps_panel.Controls.Add(step_button);
steps_panel.Load += new EventHandler(steps_panel_Load);
main_panel.Controls.Add(steps_panel);
main_panel.Load += new EventHandler(steps_panel_Load);
steps_panel.Attributes.Add("style", "float:left; width:100%");
AddStepPanel();
}
void steps_panel_Load(object sender, EventArgs e)
{
}
public void AddStep_Click(object sender, EventArgs e)
{
step_count++;
AddStepPanel();
}
public Panel GetPanel()
{
return main_panel;
}
private void AddStepPanel()
{
var panel = new StepPanel(step_count);
steps_list.Add(panel);
var button_index = steps_panel.Controls.IndexOf(step_button);
steps_panel.Controls.AddAt(button_index, panel.GetPanel());
}
private void AddStepPanel(StepPanel panel)
{
var button_index = steps_panel.Controls.IndexOf(step_button);
steps_panel.Controls.AddAt(button_index, panel.GetPanel());
}
}
public class StepPanel
{
private Panel main_panel = new Panel();
private DropDownList step_type_box = new DropDownList();
private TextBox step_name_box = new TextBox();
private TextBox step_error_box = new TextBox() ;
private Panel step_panel = new Panel();
public StepPanel(int i)
{
main_panel.Attributes.Add("style", "float:left; width:100%");
main_panel.GroupingText = "TestLevel2 " + i;
main_panel.Controls.Add(new Label() { Text = "TestLevel2" });
step_type_box.Items.Add("TestLevel2");
step_type_box.Items.Add("TestLevel22");
step_type_box.Items.Add("TestLevel222");
step_type_box.Items.Add("TestLevel2222");
step_type_box.Attributes.Add("style", "margin-left:13px");
main_panel.Controls.Add(step_type_box);
var label1 = new Label() { Text = "TestLevel22" };
label1.Attributes.Add("style","margin-left:20px;");
main_panel.Controls.Add(label1);
step_name_box.Attributes.Add("style", "margin-left:10px");
main_panel.Controls.Add(step_name_box);
var label2 = new Label() { Text = "TestLevel222" };
label2.Attributes.Add("style", "margin-left:20px;");
main_panel.Controls.Add(label2);
step_error_box.Attributes.Add("style", "margin-left:33px");
main_panel.Controls.Add(step_error_box);
}
public Panel GetPanel()
{
return main_panel;
}
}
我怎樣才能使這項工作?
'靜態int控制= 1;'意味着在您的web應用程序的每個用戶將具有相同的控制計。改用ViewState或Session。 –
[真正理解動態控件](http://tinyurl.com/yfc66o) – Oded
「static int controls = 1;意味着你的web應用程序中的每個用戶將具有相同的控制計數,改爲使用ViewState或Session。 Schmelter 10小時前「我可以使用PanelsList.Count – user1617785