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 6003475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:07:36+00:00 2026-05-23T01:07:36+00:00

I have a nested GridView that is placed inside a ListView. The GridView renders

  • 0

I have a nested GridView that is placed inside a ListView. The GridView renders perfectly fine with all the entities that are bound inside its datasource.

Each record in my gridview has two buttons – ‘delete’ and ‘edit’. The issue that I am having is that the methods wired to each of these buttons never get fired.

I think the reason for this behaviour is because my ListView’s data binding happens only on the first page load, and not on every subsequent postback. As a result, when a postback happens, the events of the nested gridview are never wired up again – hence my methods are not getting fired.

Here is what my code [simplified] looks like:

<asp:ListView ID="uiListView" ... runat="server">
    <LayoutTemplate>
        ...
    </LayoutTemplate>
    <ItemTemplate>
        ...
        <asp:GridView ID="uiGridView"  
                    OnRowDataBound="uiGridView_RowDataBound"
                    OnRowEditing="uiGridView_RowEditing"
                    OnRowDeleting="uiGridView_RowDeleting" runat="server">
            <Columns>
                ...
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="uiEditRowButton" CausesValidation="false" CommandName="Edit" Text="Edit" runat="server" />
                        <asp:Button ID="uiRemoveRowButton" CausesValidation="false" CommandName="Delete" Text="Remove" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        ...
    </ItemTemplate>
</asp:ListView>

Code Behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) {
        uiListView.ItemDataBound += new EventHandler<ListViewItemEventArgs>(uiListView_ItemDataBound);
        uiListView.DataSource = ...;
        uiListView.DataBind();
    }
}

// Find a GridView and bind relevant data to it
private void uiListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem) {
        ListViewDataItem listViewDataItem = (ListViewDataItem) e.Item;

        GridView uiGridView = (GridView)listViewDataItem.FindControl("uiGridView");
        ...
        uiGridView.DataSource = ...;
        uiGridView.DataBind();
    }
}

// For every row being bound to GridView, register the edit and delete
// buttons as postback controls
protected void uiGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Control uiEditButton = e.Row.FindControl("uiEditRowButton");
        if (uiEditButton != null) {
            ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(uiEditButton);
        }

        Control uiRemoveRowButton = e.Row.FindControl("uiRemoveRowButton");
        if (uiRemoveRowButton != null) {
            ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(uiRemoveRowButton);
        }
    }
}

// Method runs when a GridView's edit button is clicked
protected void uiGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
    Console.WriteLine('Editing Row...');
}

// Method runs when a GridView's delete button is clicked
protected void uiGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    Console.WriteLine('Deleting Row');
}

I tried modifying the above code and removing the “!IsPostBack” clause, and the events actually got fired when a button inside the GridView was clicked. However, I do not feel comfortable doing a databind on every postback and think there should be a better solution than that.

  • 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-05-23T01:07:37+00:00Added an answer on May 23, 2026 at 1:07 am

    I solved this issue but never updated this post. So for anyone interested, here is what the issue is – the wiring of the button events needs to be done in the RowCreated event handler and not the RowCommand event handler.

    Why is that? Have a look at what MSDN has to say for each of the two events:

    GridView.OnRowCreated Method:

    Before
    the GridView control can be rendered,
    a GridViewRow object must be created
    for each row in the control. The
    RowCreated event is raised when each
    row in the GridView control is
    created. This enables you to provide
    an event-handling method that performs
    a custom routine, such as adding
    custom content to a row, whenever this
    event occurs.

    GridView.OnRowDataBound Method:

    Before the GridView control can be rendered,
    each row in the control must be bound
    to a record in the data source. The
    RowDataBound event is raised when a
    data row (represented by a GridViewRow
    object) is bound to data in the
    GridView control. This enables you to
    provide an event-handling method that
    performs a custom routine, such as
    modifying the values of the data bound
    to the row, whenever this event
    occurs.

    Now what is not clear from the msdn write-up is the follows:

    OnRowCreated is triggered when a row is created by two different sources

    1. From the DataSource – that gets passed when DataBind is invoked
    2. From the ViewState – on postback, rows are loaded from the ViewState and not from data source

    So as a result, when a postback occurs, we need to bind the button events again on every row. However, because we are doing the wiring of events in the RowCommand, these methods are never being called again – because they only get fired on DataBind. What we need to do instead is wire the buttons inside the RowCreated method, which will be called when we first DataBind data to the GridView, and on every postback when rows are created from the ViewState of the GridView.

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

Sidebar

Related Questions

No related questions found

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.