首页 > 解决方案 > 如何在asp.net的gridview内的asp boundfield datafield hedertext上设置工具提示?

问题描述

我有网格视图。有两个 BoundField.here 我想在 BoundField DataField HeaderText Topic上设置工具提示。

代码。

<asp:GridView ID="Dgvlist" runat="server" >
  <Columns>
   <asp:BoundField  DataField="topic"  HeaderText="Topic"  />
   <asp:BoundField DataField="question" HeaderText="Question"  /> 
   </Columns>
</asp:GridView>    

有什么解决办法吗?

标签: asp.net

解决方案


在列上设置工具提示的常用方法有 3 种BoundField

1) 使用代码隐藏RowDataBound事件

protected void Dgvlist_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        e.Row.Cells[0].ToolTip = DataBinder.Eval(e.Row.DataItem, "Topic", string.Empty);
    }
}

2) 使用代码隐藏RowCreated事件

protected void Dgvlist_RowCreated(object sender, GridViewRowEventArgs e)
{
    foreach (TableRow row in Dgvlist.Controls[0].Controls)
    {
        row.Cells[0].ToolTip = DataBinder.Eval(e.Row.DataItem, "Topic", string.Empty);
    }
}

3) 转换为TemplateField使用Label控件

<asp:GridView ID="Dgvlist" runat="server" ...>
  <Columns>
   <asp:TemplateField HeaderText="Topic">
       <asp:Label ID="TopicID" runat="server" Text='<%# Eval("topic") %>' ToolTip='<%# Eval("topic") %>'>
       </asp:Label>
   </asp:TemplateField>
   <asp:BoundField DataField="question" HeaderText="Question"  /> 
   </Columns>
</asp:GridView>

实际实现取决于您使用的方法。

相关问题:

如何将工具提示添加到 BoundField


推荐阅读