代码之家  ›  专栏  ›  技术社区  ›  Vishal Parmar

如何在asp.net的gridview中设置asp boundfield datafield hedertext的工具提示?

  •  0
  • Vishal Parmar  · 技术社区  · 6 年前

    我有网格视图。有两个boundfield。这里我想设置boundfield datafield headerText的工具提示 话题 .

    代码。

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

    有什么解决办法吗?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Muhammad Saqlain    6 年前

    实现这一点的一个简单方法是 BoundField TemplateField 选择权。

    转换为:

    <asp:BoundField  DataField="topic"  HeaderText="Topic"  />
    

    为此:

    <asp:TemplateField HeaderText="Topic">
         <ItemTemplate>
             <asp:Label ID="Label1" runat="server" Text='<%# Bind("Topic") %>' ToolTip ='<%# Bind("Topic") %>'></asp:Label>
         </ItemTemplate>
    </asp:TemplateField>
    

    或者从代码后面你可以做 RowDataBound 像这样的事件

    protected void Dgvlist_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                e.Row.Cells[i].ToolTip = e.Row.Cells[i].Text;
            }
        }
    
        2
  •  1
  •   Tetsuya Yamamoto    6 年前

    有三种常用的方法可以设置工具提示 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>
    

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

    相关问题:

    How to add tooltip to BoundField