本文介绍了在GridView中图像按钮后面通话功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在GridView两个按钮,但他们不使用的CommandName,其中我写的函数名后返回任何事件,但它不会做任何事情。

该屏幕快照如下:

这两个按钮是编辑和删除。

这里的片段:

 < ASP:ButtonField字段按钮类型=形象的ImageUrl =〜/可湿性粉剂内容/主题/教具/资产/ IMG /编辑铅笔的变化修改修改蓝色编辑icon2.png的CausesValidation =真正的文本=编辑的CommandName =Edit_data>
                < ControlStyle HEIGHT =30像素WIDTH =30像素/>
                < / ASP:ButtonField字段>
< ASP:ButtonField字段按钮类型=形象的ImageUrl =〜/可湿性粉剂内容/主题/教具/资产/ IMG / 500像素,Delete_Icon2.png文本=删除>
                < ControlStyle HEIGHT =30像素WIDTH =30像素/>
                < / ASP:ButtonField字段>

C#:

 保护无效Edit_data(对象发件人,EventArgs的发送)
    {
        的Response.Write(你好);
    }


解决方案

 < ASP:GridView控件ID =gvProduct=服务器的AutoGenerateColumns =FALSE的DataKeyNames =ID 
                                    OnRowCommand =gvProduct_RowCommandWIDTH =100%>
                                    <柱体和GT;
                                        < ASP:的TemplateField>
                                            <&ItemTemplate中GT;
                                                < ASP:ImageButton的ID =btnEdit=服务器的CommandName =EditCommand的ImageUrl =〜/图片/表格/ edit.png
                                                     />
                                            < / ItemTemplate中>
                                                                                        < / ASP:的TemplateField>
                                        < ASP:BoundField的数据字段=ProjectNo/>
                                        < ASP:BoundField的数据字段=OrderLetterNo/>
                                        < ASP:BoundField的数据字段=日期/>
                                        < ASP:BoundField的数据字段=轿车/>
                                        < ASP:的TemplateField>
                                            <&ItemTemplate中GT;
                                                < ASP:ImageButton的ID =btnDelete=服务器的CommandName =DeleteCommand会的ImageUrl =〜/图片/表格/ delete.png/>
                                            < / ItemTemplate中>
                                                                                        < / ASP:的TemplateField>
                                    < /专栏>
                                    < / ASP:GridView的>

和这里是code背后:

 保护无效gvProduct_RowCommand(对象发件人,GridViewCommandEventArgs E)
    {
        GridViewRow行=(GridViewRow)((控制)e.CommandSource).NamingContainer;
        INT ROWID = Convert.ToInt32(gvProduct.DataKeys [Row.RowIndex] .value的);        如果(e.CommandName ==EditCommand)
        {
            EditFunction(ROWID);
        }
        其他
            如果(e.CommandName ==的DeleteCommand)
            {
                DeleteFunction(ROWID);
            }
    }

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.

the screen shot is as follow:

the two buttons are edit and delete.

here is the snippet:

<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");
    }
解决方案
<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);
            }
    }

这篇关于在GridView中图像按钮后面通话功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 17:05