Ajax Update Panel asp.net触发器

时间:2013-06-07 08:37:46

标签: c# asp.net ajax

我有3个更新面板控件asp.net updatePanel1updatePanel2updatePanel3。首次加载页面updatePanel1会自动加载更多buttons例如:button1。点击button1中的updatePanel1,会在updatePanel2例如:button2中显示按钮。点击button2后,它会触发updatePanel3并在updatePanel3中显示数据,如gridview。

现在,我的问题是,点击button2 updatePanel2中的所有按钮都会丢失。你能给我解决为什么我在updatePanel2丢失按钮的原因吗?

UPDATE

Default.cs

    protected void Page_Load(object sender, EventArgs e)
    {


        foreach (KeyValuePair<String, String> catshow in cat)
        {
            Button x = new Button();
            x.ID = catshow.Key;
            x.CssClass = "btnTop";
            x.Text = catshow.Value;
            x.CommandArgument = catshow.Key;
            x.Command += new CommandEventHandler(buttonClick);

            PlaceHolder1.Controls.Add(x);
        }


    }

    protected void buttonClick(object sender, CommandEventArgs e)
    {
        string key = e.CommandArgument.ToString();
        foreach (KeyValuePair<String, String> datshow in data[key])
        {
            Button x = new Button();
            x.ID = datshow.Key;
            x.CssClass = "btnBottom";
            x.Text = datshow.Value;
            x.CommandArgument = datshow.Key;
            x.Command += new CommandEventHandler(buttonClickPrd);

            PlaceHolder2.Controls.Add(x);
        }
    }


    protected void buttonClickPrd(object sender, CommandEventArgs e)
    {
        string key = e.CommandArgument.ToString();
        DataTable dt = new DataTable();
        dt.Columns.Add("Qty", Type.GetType("System.String"));
        dt.Columns.Add("Unit", Type.GetType("System.String"));
        dt.Columns.Add("Price", Type.GetType("System.String"));
        dt.Columns.Add("Total", Type.GetType("System.String"));
        dt.Rows.Add();
        dt.Rows[dt.Rows.Count - 1]["Qty"] = this.sales[key]["qty"];
        dt.Rows[dt.Rows.Count - 1]["Unit"] = this.sales[key]["unit"];
        dt.Rows[dt.Rows.Count - 1]["Price"] = this.sales[key]["price"];
        dt.Rows[dt.Rows.Count - 1]["Total"] = this.sales[key]["total"];
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

Default.aspx

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
             <ContentTemplate>
                 <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
            </ContentTemplate>
         </asp:UpdatePanel>
         <asp:UpdatePanel ID="UpdatePanel2" runat="server">
             <ContentTemplate>
                 <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
             </ContentTemplate>
         </asp:UpdatePanel>
         <asp:UpdatePanel ID="UpdatePanel3" runat="server">
             <ContentTemplate>
                 <asp:GridView ID="GridView1" runat="server"></asp:GridView>
             </ContentTemplate>
         </asp:UpdatePanel>

1 个答案:

答案 0 :(得分:1)

这与viewstate有关。因为updatepanel2中的按钮是在运行时创建的,所以它们丢失的是page_load事件。您每次都必须重新生成。具体如何实现它取决于你如何编写代码。没有代码,我不能告诉你更多。