Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8186243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:07:39+00:00 2026-06-07T02:07:39+00:00

I can’t figure out how to programmatically add a GridView with buttons to an

  • 0

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

I can do it with simple controls such as buttons and labels, but when I try to add a GridView with buttons a full Postback() occurs.

<%@ 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 causing a full PostBack() if the GridView is clicked?

Edit: Other things I have 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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:07:40+00:00Added an answer on June 7, 2026 at 2:07 am

    Well according to:

    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

    Basically I used your code with small changes:

    • Removed the binding from the Init event and I execute it in the Load event

    • The UpdatePanel is created at design time with a nested panel, and you simply add your dynamic controls to that panel

    This code will do it (it works on 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 behind

        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();
            }
        }
    

    Output

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can't figure out how to do this in a pretty way : I have
Can't work out a way to make an array of buttons in android. This
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can any one tell me clearly why ? Reading is simple in ArrayList,( we
Can anyone help me trying to find out why this doesn't work. The brushes
Can someone please explain to me in a simple way why constants don't matter
Can anyone let me know how can we change the value of kendo combobox
Can I order my users in the database, so I don't have to say
Can I change the field public virtual ClassOne ClassOne { get; set; } to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.