En el botón Haga clic en la cuadrícula No puedo acceder a la función

Using CSharp, I'm not getting into the (GridView1_RowDeleting) function on the button click.. I don't know whats the problem is but wasted alot of time on it.

Default.aspx

asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting"
    AllowPaging="True">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton  ID="img1" runat="server" CommandName="GridView1_RowDeleting"  ImageUrl="~/Images/cross.png" />

            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Predeterminado.aspx.cs

 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

}

preguntado el 31 de julio de 12 a las 14:07

Is GridView1 being data bound programmatically? If so, are you data-binding the grid with the same data on every postback? -

Did you type the delete button manually? The command name parameter is wrong. It is the type of command the button issues, no the name of an event handler. Add a command handler to the grid view, and check it's command name argument for which button was clicked. -

I wrote the button manually but the function in the aspx.cs is automatically made by the properties rowdeleting function. -

@mellamokb I'm not binding the grid with the same data on every post back. -

3 Respuestas

Utilice las OnItemCommand:

protected void Test(object sender,DataGridEventArgs e) 
{ 
   if(e.CommandName == "GridView1_RowDeleting")
   {
       // do something
   }
}

<asp:GridView ID="GridView1"  runat="server" OnItemCommand="Test" AllowPaging="True">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton  ID="img1"
                                  runat="server"
                                  CommandName="GridView1_RowDeleting"
                                  ImageUrl="~/Images/cross.png" />

            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Respondido 31 Jul 12, 14:07

You have to use the OnCommand property of ImageButton:

Respondido 31 Jul 12, 14:07

Please update your code with following.

Escribe. OnCommand="GridView1_RowDeleting"

Espero que funcione.

protected void GridView1_RowDeleting(object sender, CommandEventArgs e)
{
    // Do something      
}

Respondido 31 Jul 12, 21:07

Error 40 No overload for 'GridView1_RowDeleting' matches delegate 'System.Web.UI.WebControls.CommandEventHandler' - Fahad

Did you pass command argument giving it your unique id and did you remove the commandname. Use only OnCommand dont use commandname. Instead use CommandArgument='<%#DataBinder.Eval(Container.DataItem, "UniqueID")%>' - Ruchi

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.