Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well according to:</p> <blockquote> <p>And I don't mind having the update panel created at the design time. I just need to be able to add stuff (like tables that contain gridviews that contain buttons into it) programmatically and then be able to do a partial postback</p> </blockquote> <p>Basically I used your code with small changes:</p> <ul> <li><p>Removed the binding from the <code>Init</code> event and I execute it in the <code>Load</code> event</p></li> <li><p>The <code>UpdatePanel</code> is created at design time with a nested panel, and you simply add your dynamic controls to that panel</p></li> </ul> <p>This code will do it (it works on my environment):</p> <h3>ASPX</h3> <pre><code> &lt;asp:ScriptManager runat="server" /&gt; &lt;asp:UpdatePanel runat="server"&gt; &lt;ContentTemplate&gt; &lt;asp:Panel runat="server" ID="myPanel"&gt; &lt;/asp:Panel&gt; &lt;br /&gt; &lt;asp:Label runat="server" ID="lblMessage"&gt;&lt;/asp:Label&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; </code></pre> <h3>Code behind</h3> <pre><code> 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&lt;Product&gt;.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(); } } </code></pre> <h3>Output</h3> <p><img src="https://i.stack.imgur.com/57ABI.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload