在GridView中的图像按钮后面调用函数

时间:2014-11-11 09:25:48

标签: c# asp.net gridview

我在gridview中有两个按钮但是在使用“CommandName”之后它们没有返回任何事件,我在其中写了函数名但它没有做任何事情。

屏幕截图如下: enter image description here

两个按钮是编辑和删除。

这是片段:

<asp:ButtonField ButtonType="Image" ImageUrl="~/wp-content/themes/realia/assets/img/edit pencil change modify alter blue edit icon2.png" CausesValidation="true" Text="Edit" CommandName="Edit_data">
                <ControlStyle Height="30px" Width="30px" />
                </asp:ButtonField>
<asp:ButtonField ButtonType="Image" ImageUrl="~/wp-content/themes/realia/assets/img/500px-Delete_Icon2.png" Text="Delete">
                <ControlStyle Height="30px" Width="30px" />
                </asp:ButtonField>

C#:

protected void Edit_data(object sender, EventArgs e)
    {
        Response.Write("hello");
    }

1 个答案:

答案 0 :(得分:1)

<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>

以下是代码:

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