9

当用户将鼠标悬停在 gridview 中列的列标题上时,例如: Column Heading Year,当我将鼠标悬停在 Year 上时,我应该看到该年份的解释“这是学生加入大学的年份等”。

下面是我的 ascx 代码:

 <asp:GridView ID="grdView" runat="server" Width="900px" AutoGenerateColumns="False"
                AllowPaging="true" AllowSorting="true" CellSpacing="0" CellPadding="5" PageSize="20"
        OnRowDataBound="grdView_RowDataBound">
                <Columns>
 <asp:TemplateField HeaderText="ID Number" ItemStyle-Width="90px" >
    <ItemTemplate>
      <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID")%'></asp:Label>
    </ItemTemplate>
 </asp:TemplateField><asp:BoundField DataField="StudentName" HeaderText="StudentName"> </asp:BoundField>

请让我知道如何将鼠标悬停在网格视图的列标题上的文本或工具提示上。谢谢,

4

6 回答 6

10

这是 C# 中使用 Jquery 的 Gridview 的 CSS 工具提示

protected void grd_popup_details_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;
        }
    }

参考链接

于 2015-05-27T12:31:48.680 回答
8

我从来没有做过任何asp.net开发,但是这里似乎提供了一个解决方案:how to add title for each header column in gridview in ASP.NET

您的示例可能如下所示:

 <asp:GridView ID="grdView" runat="server" Width="900px" AutoGenerateColumns="False"
            AllowPaging="true" AllowSorting="true" CellSpacing="0" CellPadding="5" PageSize="20"
    OnRowDataBound="grdView_RowDataBound">
            <Columns>
 <asp:TemplateField HeaderText="ID Number" ItemStyle-Width="90px" >
<HeaderTemplate>
       <asp:Label ID="Header" ToolTip="HERE WE GO!!!!" runat="server" Text="Label"></asp:Label>
       </HeaderTemplate>
    <ItemTemplate>
      <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID")%'></asp:Label>
    </ItemTemplate>
 </asp:TemplateField><asp:BoundField DataField="StudentName" HeaderText="StudentName"> </asp:BoundField>

我会试试看:)

于 2012-11-08T22:20:23.637 回答
5

在后面的代码中,为 GridView 创建方法 rowDataBound 并添加以下代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        foreach (TableCell cell in e.Row.Cells)
        {
            cell.Attributes.Add("title", "Tooltip text for " + cell.Text);
        }
    }
}

不要忘记在 GridView 中设置属性 OnRowDataBound。

http://rosshawkins.net/archive/2007/04/15/adding-tooltips-to-gridview-headers.html.aspx

于 2013-11-01T17:37:55.213 回答
1

这是一个示例,显示即使在 Autogenerate=True 并且动态确定 GridView 列时也可以使用 ColumnName。

当文本包含转义字符(例如双引号)时,似乎也需要 HtmlDecode() 。

Dictionary<string, int> _headerIndiciesForDetailsReportGridView = null;

protected void detailsReportGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (_headerIndiciesForDetailsReportGridView == null)
    {
        int index = 0;
        _headerIndiciesForDetailsReportGridView = ((Table)((GridView)sender).Controls[0]).Rows[0].Cells
            .Cast<TableCell>()
            .ToDictionary(c => c.Text, c => index++);
    }

    if (e.Row.RowType == DataControlRowType.DataRow)
    {                            
        TableCell cell = e.Row.Controls[_headerIndiciesForDetailsReportGridView["theColumnName"]] as TableCell;

        // Shorten text in a particular column to a max of 20 characters.
        // Set tooltip to the full original text. Decode to remove &quot, etc.
        //
        string orgText = cell.ToolTip = HttpUtility.HtmlDecode(cell.Text);

        if (orgText.Length > 20)    // If cell text should be shortened
            cell.Text = HttpUtility.HtmlEncode(orgText.Substring(0, 20) + "...");
    }
}
于 2017-07-29T00:16:36.537 回答
1
if (e.Row.RowType == DataControlRowType.DataRow)
{
     e.Row.Cells[1].ToolTip = Grd.Columns[1].HeaderText;
}
于 2018-06-11T05:42:59.837 回答
0
 <ItemTemplate>                                                                               <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Bind("EmpInfoDetail") %>' />                                                                                                                                                              <asp:Label ID="Label3" runat="server" Text="" Visible="false" Font-Bold="true" ForeColor="#cc3300" CssClass="tooltip"></asp:Label>                                                                                                                                                                                                                                                                                         </ItemTemplate> 
    


     use this code on grid view row data bound   
        #region show grid text on mouse hover
                        if (e.Row.RowType == DataControlRowType.DataRow)
                        {
                            DataRowView drv = e.Row.DataItem as DataRowView;
                            Label test = e.Row.FindControl("Label3") as Label;
                            if (drv["EmpInfoDetail"].ToString().Length > 500)
                            {
                                test.Text = drv["EmpInfoDetail"].ToString().Substring(0, 500) + "...";
                            }
                            else
                            {
                                test.Text = drv["EmpInfoDetail"].ToString();
                            }
            
                            e.Row.Cells[1].ToolTip = drv["EmpInfoDetail"].ToString();
            
                        }
                        #endregion
于 2020-08-06T05:01:04.423 回答