Hi,

I have a Button inside of a GridView, inside of another GridView.

The higher gv is filled with a dataset, and in the RowCreated event I fill the second gv, and finally, in the RowCreated event (handler added in the RowCreated event of the first gv) of the second gv, attach the handler of the Click event with the button inside.

I have another controls in the same way with their own events added and works fine, but this Button dont... When I press the button, it postback but the event is like not been called (even when I debugged and the rowcreated reach the line with the addhandler for the button)

Maybe some one can guide me through this...

aspx

<asp:Content ID="cVentanillaAtencionFlujo" ContentPlaceHolderID="cphVentanillaAtencionMaster" Runat="Server">
    <asp:ScriptManager ID="smVentanillaAtencionFlujo" runat="server">
    </asp:ScriptManager>

        <asp:GridView BackColor="LightGray" ShowHeader="False" ID="gvFasesListar" runat="server" AutoGenerateColumns="False"
            Width="728px" DataKeyNames="inFasCodigo">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
<table cellpadding="0" cellspacing="0" style="width: 672px; background-color:#FFFFFF">
                                                    <tr>
<td align="right">
                                                            <asp:Button ID="btnActualizarResultadosTest" CausesValidation="false" runat="server" Text="Actualizar" Width="120px" />
                                                        </td>
                                                    </tr>
                                                </table>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
                        
</asp:Content>

Code behind

If e.Row.RowType = DataControlRowType.DataRow Then
                        Dim btnActualizarResultadosTest As New WebControls.Button
                        btnActualizarResultadosTest = e.Row.FindControl("btnActualizarResultadosTest")
                        If Not btnActualizarResultadosTest Is Nothing Then
                            'AddHandler btnActualizarResultadosTest.Click, AddressOf btnActualizarResultadosTest_Click
                            AddHandler btnActualizarResultadosTest.Command, AddressOf btnActualizarResultadosTest_Command
                        End If
                    End If

Dani AI

Generated

Nested-GridView button handlers almost always fail because of control-lifecycle and wiring timing, not because the Button itself is broken. In this thread is attaching handlers in RowCreated — that can work, but only if the inner GridView rows are recreated and the handlers reattached on every postback before ASP.NET raises postback events. ’s PostBackUrl suggestion will not help (and can prevent the normal server postback), while ’s advice to use CommandName/RowCommand is the most robust and simplest approach.

Quick troubleshooting checklist

  • Ensure the inner GridView is recreated/bound on every postback (or at least its child controls are recreated in OnInit) so FindControl returns the same server control instance that caused the postback.
  • Attach handlers every time rows are created (RowDataBound is a safe place), not only on initial load.
  • Don’t instantiate a new Button before calling FindControl — that’s unnecessary and confusing.
  • If using AddHandler, confirm the handler signature matches the event (Click -> EventArgs, Command -> CommandEventArgs).
  • If UpdatePanel/ScriptManager are involved, make sure triggers or panel scope allow the partial postback.

Recommended, simpler fix
Use a CommandName/CommandArgument on the template button and handle the GridView’s RowCommand (no per-row AddHandler required). Example pattern:

<!-- template -->
<asp:Button ID="btnAct" runat="server" CommandName="Actualizar" CommandArgument='<%# Container.DataItemIndex %>' Text="Actualizar" />

' code-behind
Protected Sub innerGv_RowCommand(sender As Object, e As GridViewCommandEventArgs)
    If e.CommandName = "Actualizar" Then
        Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
        ' locate DataKey or row and perform the update
    End If
End Sub

Using RowCommand eliminates most wiring/timing bugs. If AddHandler is still preferred, recreate and reattach on every postback (early in the lifecycle) and check the exact event/signature.

Recommended Answers

All 3 Replies

Try:

buttonName.PostBackUrl="javascript:void(0);"

Thanks... but I tried it, and It didn't worked. I just have to give up that idea, coz I'm on a schedule...

Thank you very much...

Just go to design view. Open the template column then add the event in design view. Set commandname when bind data. Now in the click event catch the command value & do the next job.

The way you used is basically needed when you create dynamic control otherwise try to avoid unnecessary complexity.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.