将按钮列“删除”替换为在 Gridview 中使用图像按钮

发布于 2024-12-02 13:06:31 字数 432 浏览 4 评论 0原文

这是我的 Gridview 代码,

<asp:DataGrid id="dg" runat="server" ondeletecommand="Delete_Item" >

        <columns>

        <asp:buttoncolumn buttontype="LinkButton" commandname="Delete" text="Remove" />

        </columns>
            <HeaderStyle BackColor="#95C736" ForeColor="White" Font-Bold="True" />
        </asp:DataGrid>

我想用图像替换我的按钮列,我必须对我的 GridView 做什么?

This is my Gridview code

<asp:DataGrid id="dg" runat="server" ondeletecommand="Delete_Item" >

        <columns>

        <asp:buttoncolumn buttontype="LinkButton" commandname="Delete" text="Remove" />

        </columns>
            <HeaderStyle BackColor="#95C736" ForeColor="White" Font-Bold="True" />
        </asp:DataGrid>

I want to replace my buttoncolumn with an image, what must I do to my GridView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷雾森÷林ヴ 2024-12-09 13:06:31

我将为此使用模板列:

<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="/images/delete.png" CommandName="Delete" />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid> 

I would use a template column for this:

<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="/images/delete.png" CommandName="Delete" />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid> 
转瞬即逝 2024-12-09 13:06:31

将 Button Column 替换为 TemplateColumn 允许您将标准 asp 控件放入其中。然后就可以正常处理Datagrid_ItemCommand事件了。

    <asp:DataGrid ID="dgTest" runat="server">
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:ImageButton ID="ibtnDelete" runat="server" CommandName="cmdDelete"
                        ImageUrl="~/images/delete.png" />
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
    </asp:DataGrid>

ItemCommand 处理程序类似于:

Protected Sub dgTest_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgTest.ItemCommand
    If (e.CommandName = "cmdDelete") Then
        Response.Write("the command argument was :" & e.CommandArgument)
    End If
End Sub

您需要做的唯一另一件事是将一些数据绑定到命令参数的图像按钮。我通常会这样做:

Protected Sub dgTest_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgTest.ItemDataBound
    If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim di As FAQTable = e.Item.DataItem
        DirectCast(e.Item.FindControl("ibtn"), ImageButton).CommandArgument = di.FAQID
    End If
End Sub

您还可以使用 asp:image 控件和 asp:Hyperlink 控件来获得相同的结果。

Replace the Button Column with a TemplateColumn with allows you to put standard asp controls inside. Then you can handle the Datagrid_ItemCommand event normally.

    <asp:DataGrid ID="dgTest" runat="server">
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:ImageButton ID="ibtnDelete" runat="server" CommandName="cmdDelete"
                        ImageUrl="~/images/delete.png" />
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
    </asp:DataGrid>

The ItemCommand handler would be something like:

Protected Sub dgTest_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgTest.ItemCommand
    If (e.CommandName = "cmdDelete") Then
        Response.Write("the command argument was :" & e.CommandArgument)
    End If
End Sub

The only other thing you would need to do is bind some data to the image button for a command argument. I usually do something like:

Protected Sub dgTest_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgTest.ItemDataBound
    If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim di As FAQTable = e.Item.DataItem
        DirectCast(e.Item.FindControl("ibtn"), ImageButton).CommandArgument = di.FAQID
    End If
End Sub

You could also use an asp:image control with an asp:Hyperlink control to get the same results.

假情假意假温柔 2024-12-09 13:06:31

我刚刚做的一件非常棒的事情就是将编码的 html 放入文本中。 Text="<img src='images/btn-find.png' class='ttip' alt='查看详细信息' />" 这让我知道只输入img src,还指定一个类和一个 alt 标签。

您需要做的就是使用单刻度线并使用 gtlt 对您的 <> 进行编码。

One thing I just did that worked awesome is to put encoded html into the text. Text="<img src='images/btn-find.png' class='ttip' alt='View Details' />" This let me know only put in the img src, but also specify a class and an alt tag.

All you need to do is use single tick marks and encode your <> with gt and lt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文