How do you add gridview programmatically in UpdatePanel?

I can't figure out how to programmatically add a GridView with buttons to an UpdatePanel .

I can do this with simple controls like buttons and labels, but when I try to add a GridView with buttons, full Postback() appears.

 <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected override void OnInit(EventArgs e) { UpdatePanel up1 = new UpdatePanel(); up1.ID = "UpdatePanel1"; Button button1 = new Button(); button1.ID = "Button1"; button1.Text = "Submit"; button1.Click += new EventHandler(Button_Click); Label label1 = new Label(); label1.ID = "Label1"; label1.Text = "A full page postback occurred."; GridView gv1 = new GridView(); //Where the xml gets bonded to the data grind XmlDataSource xds = new XmlDataSource(); xds.Data = xml; xds.DataBind(); xds.EnableCaching = false; gv1.DataSource = xds; createButton(gv1, up1); gv1.RowCommand += new GridViewCommandEventHandler(CustomersGridView_RowCommand); gv1.DataBind(); up1.ChildrenAsTriggers = true; up1.ContentTemplateContainer.Controls.Add(button1); up1.ContentTemplateContainer.Controls.Add(label1); up1.ContentTemplateContainer.Controls.Add(gv1); Page.Form.Controls.Add(up1); } protected void Page_Load(object sender, EventArgs e) { } public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { if (e.CommandName == "buttonClicked") { int index = Convert.ToInt32(e.CommandArgument); } } void createButton(GridView g) { ButtonField tea = new ButtonField(); tea.ButtonType = ButtonType.Image; tea.ImageUrl = "~/checkdailyinventory.bmp"; tea.CommandName = "buttonClicked"; tea.HeaderText = "space"; g.Columns.Add(tea); } protected void Button_Click(object sender, EventArgs e) { ((Label)Page.FindControl("Label1")).Text = "Panel refreshed at " + DateTime.Now.ToString(); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>UpdatePanel Constructor Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button2" runat="server" Text="Button" /> <asp:ScriptManager ID="ScriptManager1" runat="server" /> </div> </form> </body> </html> 

So, how do you add a gridview with buttons programmatically to an UpdatePanel without calling a full Postback() if the GridView button is pressed?

Edit: Other things I've tried

  void gv1_RowDataBound(object sender, GridViewRowEventArgs e) { AsyncPostBackTrigger t = new AsyncPostBackTrigger(); t.ControlID = e.Row.Cells[0].ClientID; t.EventName = "blah"; up1.Triggers.Add(t); } 
+6
source share
2 answers

Good according to:

And I do not mind creating an update panel created during development. I just need to be able to add things (like tables containing gridviews containing buttons) programmatically, and then be able to do partial postback

I mainly used your code with slight modifications:

  • The binding is removed from the Init event, and I execute it in the Load event

  • UpdatePanel is created at design time with a sub-panel, and you simply add your dynamic controls to this panel

This code will do this (it works in my environment):

Aspx

  <asp:ScriptManager runat="server" /> <asp:UpdatePanel runat="server"> <ContentTemplate> <asp:Panel runat="server" ID="myPanel"> </asp:Panel> <br /> <asp:Label runat="server" ID="lblMessage"></asp:Label> </ContentTemplate> </asp:UpdatePanel> 

Code for

  protected void Page_Init(object sender, EventArgs e) { Button button1 = new Button(); button1.ID = "Button1"; button1.Text = "Submit"; button1.Click += new EventHandler(Button_Click); Label label1 = new Label(); label1.ID = "Label1"; label1.Text = "A full page postback occurred."; var s1 = Builder<Product>.CreateListOfSize(15).Build(); GridView gv1 = new GridView(); gv1.DataSource = s1; createButton(gv1); gv1.RowCommand += new GridViewCommandEventHandler(CustomersGridView_RowCommand); this.myPanel.Controls.Add(button1); this.myPanel.Controls.Add(label1); this.myPanel.Controls.Add(gv1); } void createButton(GridView g) { ButtonField tea = new ButtonField(); tea.ButtonType = ButtonType.Image; tea.ImageUrl = "~/checkdailyinventory.bmp"; tea.CommandName = "buttonClicked"; tea.HeaderText = "space"; g.Columns.Add(tea); } protected void Button_Click(object sender, EventArgs e) { ((Label)Page.FindControl("Label1")).Text = "Panel refreshed at " + DateTime.Now.ToString(); } protected void Page_Load(object sender, EventArgs e) { this.DataBind(); } public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { if (e.CommandName == "buttonClicked") { //int index = Convert.ToInt32(e.CommandArgument); this.lblMessage.Text = DateTime.Now.ToString(); } } 

Exit

enter image description here

+2
source

Could you just put the GridView there during development and just hide it by setting Visible=false ?

If you don't know how many gridviews you need to repeat, you can wrap the GridView in a ListView. The concept is introduced here:

This may not be the perfect solution. I just thought that I propose to see him, because there is generosity, I assume that you have painted a brick wall so far.

+2
source

Source: https://habr.com/ru/post/919594/


All Articles