Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you add a Gridview programmatically to an UpdatePanel
    text
    copied!<p>I can't figure out how to programmatically add a <code>GridView</code> with buttons to an <code>UpdatePanel</code>.</p> <p>I can do it with simple controls such as buttons and labels, but when I try to add a <code>GridView</code> with buttons a full <code>Postback()</code> occurs.</p> <pre><code>&lt;%@ Page Language="C#" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;script runat="server"&gt; 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(); } &lt;/script&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head id="Head1" runat="server"&gt; &lt;title&gt;UpdatePanel Constructor Example&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:Button ID="Button2" runat="server" Text="Button" /&gt; &lt;asp:ScriptManager ID="ScriptManager1" runat="server" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>So how do you add a gridview with buttons programmatically to an <code>UpdatePanel</code> without causing a full <code>PostBack()</code> if the <code>GridView</code> is clicked?</p> <p>Edit: Other things I have tried</p> <pre><code> 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); } </code></pre>
 

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