1

我有一个动态页面,其中包含一个包含内容的面板。我在单击按钮时添加了一个新面板。使用动态添加的面板中的按钮,单击事件不会触发。

这是表单的代码:

<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;
    }

}

我怎样才能使这项工作?

4

2 回答 2

0

我解决了我的问题。

我使用保存在 Session 中的面板是我的错误。所有动态控件都必须在 Postback 中重新创建。

所以我更新方法“AddActionPanel(ActionPanel 面板)”:

    protected void AddActionPanel(ActionPanel panel)
    {
        var count = PanelsList.IndexOf(panel);
        var panel1 = new ActionPanel(count, ScriptManager1);

        var button_index = DataActionsPanel.Controls.IndexOf(button);
        DataActionsPanel.Controls.AddAt(count, panel1.GetPanel());
    }
于 2012-08-23T10:45:46.900 回答
0

您正在添加面板onload,请在init或上进行操作preinit。这是微软的建议。

于 2012-08-22T18:25:51.553 回答