简体   繁体   中英

Ajax Update Panel asp.net trigger

I have 3 update panel control asp.net which updatePanel1 , updatePanel2 , updatePanel3 . When page load first updatePanel1 will automatically load more buttons ex: button1 . When click button1 in updatePanel1 , that will show buttons in updatePanel2 ex: button2 . When click button2 it will trigger updatePanel3 and show data in updatePanel3 like gridview.

Now, what is my problem is when click button2 all buttons in updatePanel2 lose. can you give me solution why I lose buttons in 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>

this has to do with the viewstate. since buttons in updatepanel2 are created at runtime, they get lost is page_load event. You will have to regenerate each time. Exactly how you will implement it depends upon how you have written you code. without code I cant tell you more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM