call function behind the image button in GridView

前端 未结 1 1868
死守一世寂寞
死守一世寂寞 2020-12-22 10:27

i am having two buttons in the gridview but they dont return anything event after using the \"CommandName\" in which i write the function name but it does not do anything.

相关标签:
1条回答
  • 2020-12-22 11:08
    <asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" 
                                        OnRowCommand="gvProduct_RowCommand" Width="100%">
                                        <Columns>
                                            <asp:TemplateField>
                                                <ItemTemplate>
                                                    <asp:ImageButton ID="btnEdit" runat="server" CommandName="EditCommand" ImageUrl="~/Images/Grid/edit.png"
                                                         />
                                                </ItemTemplate>
                                                                                            </asp:TemplateField>
                                            <asp:BoundField DataField="ProjectNo" />
                                            <asp:BoundField DataField="OrderLetterNo"  />
                                            <asp:BoundField DataField="Date"  />
                                            <asp:BoundField DataField="Saloon" />
                                            <asp:TemplateField>
                                                <ItemTemplate>
                                                    <asp:ImageButton ID="btnDelete" runat="server" CommandName="DeleteCommand" ImageUrl="~/Images/Grid/delete.png" />
                                                </ItemTemplate>
                                                                                            </asp:TemplateField>
                                        </Columns>
                                        </asp:GridView>
    

    and here is the code behind:

    protected void gvProduct_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow Row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
            int rowID = Convert.ToInt32(gvProduct.DataKeys[Row.RowIndex].Value);
    
            if (e.CommandName == "EditCommand")
            {
                EditFunction(rowID);
            }
            else
                if (e.CommandName == "DeleteCommand")
                {
                    DeleteFunction(rowID);
                }
        }
    
    0 讨论(0)
提交回复
热议问题