如何在Gridview标题中添加图像按钮和文本 [英] how to add Image button and text in Gridview header

查看:87
本文介绍了如何在Gridview标题中添加图像按钮和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在每个标题列上添加一个带有文本的图像按钮,以便用户可以点击预订该项目。

Hello i want to add an imagebutton on each header column with text so that user can click to book that item.

推荐答案

这个怎么样



http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datacontrolfield.headerimageurl(v = vs.110).aspx [ ^ ]


您可以添加模板列并为其添加链接按钮。如果您想要图像,可以使用图像按钮。如果你想要图像和文本,那么你可以在链接按钮中嵌入图像标签。

将命令参数属性与项目的ID绑定并处理网格视图的RowCommand事件。 />


网格视图:



< asp:GridView ID =GridView1runat =server>

< Columns>

< asp:TemplateField HeaderText =Book>

< ItemTemplate>



< asp:LinkBut​​ton ID =LinkBut​​ton1runat =server

CommandArgument ='<%#Bind(ID)%>'的CommandName = bookitem >

< asp:Image ID =Image1runat =serverImageUrl =〜/ Images / Filter.gif/>预订此商品< / asp:LinkBut​​ton>

< / ItemTemplate>

< / asp:TemplateField>

< / Columns>

< / asp:GridView>



代码落后:



受保护的子GridView1_RowCommand(发送者为对象,e为System.Web.UI.WebControls.GridViewCommandEventArgs)处理GridView1.RowCommand

如果e.CommandName =bookitem那么

' '在这里添加代码

''你可以使用e.CommandArgument获取CommandArgument

结束如果

End Sub
You can add a template column and add link button to it. If you want an image you can use image button. If you want both image and text, then you can embed images tag inside the link button.
Bind the command argument property with the ID of the item and handle the RowCommand event of the grid view.

Grid view:

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="Book">
<ItemTemplate>

<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Bind("ID") %>' CommandName="bookitem">
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/Filter.gif" /> Book this item</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Code behind:

Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "bookitem" Then
'' add code here
'' you can use e.CommandArgument to get the CommandArgument
End If
End Sub


你可以使用HeaderTemplate并添加任何控件...



就像在第二个解决方案中使用的ItemTemplate一样..



了解更多关于HeaderTemplate 这里 <无线电通信/>




You can use HeaderTemplate and add any control to it...

Just like ItemTemplate used in second solution..

read more about HeaderTemplate Here


<asp:gridview id="GridView1" 
        width="250" 
        runat="server">

        <columns>
          <asp:templatefield>
            
            <headertemplate>
              <asp:ImageButton ID="btn1" runat="server" ImageUrl="img1.gif" OnClick="btn1_Click"/>
            </headertemplate>
          </asp:templatefield>                      
        </columns>

      </asp:gridview>


这篇关于如何在Gridview标题中添加图像按钮和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆